2

So I'm using DataContext as the connection to DB. I want to get all companies from DB, with the exception of the deleted ones. My Company object:

[Table(Name = "Companies")]
class Company {
    [Column(IsPrimaryKey = true, IsDbGenerated = true)]
    public int Id { get; set; }
    [Column]
    public bool Deleted { get; set; }
    [Column]
    public DateTime DateCreated { get; set; }
    [Column]
    public DateTime? DateModified { get; set; }
    [Column]
    public string Name { get; set; }

    private EntitySet<Platform> _Platforms;
    [Association(Storage = "_Platforms", OtherKey = "CompanyId")]
    public virtual EntitySet<Platform> Platforms {
        get {
            return this._Platforms;
        }
        set {
            this._Platforms.Assign(value);
        }
    }

    public Company() {
        this._Platforms = new EntitySet<Platform>();
    }

}

My DataContext object:

class DataManager : DataContext {

    public Table<Company> Companies {
        get {
            return this.GetTable<Company>();
        }
    }
//other tables emited
}

And when I want to get the companies(all except the deleted) I use this:

List<Company> comp = _db.Companies.Where(x => !x.Deleted).ToList();

_db is created separately and is not NULL at this point.

And I get an exception on the "!x.Deleted" part: "InvalidCastException: Specified cast is not valid."

Stack trace:

   at System.Data.SqlClient.SqlBuffer.get_Boolean()
   at System.Data.SqlClient.SqlDataReader.GetBoolean(Int32 i)
   at Read_Company(ObjectMaterializer`1 )
   at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at GameShop.Form1.UpdateCompanyList() in c:\Users\Xpym\Documents\Visual Studio 2012\Projects\GameShop\GameShop\Form1.cs:line 117
   at GameShop.Form1..ctor() in c:\Users\Xpym\Documents\Visual Studio 2012\Projects\GameShop\GameShop\Form1.cs:line 20
   at GameShop.Program.Main() in c:\Users\Xpym\Documents\Visual Studio 2012\Projects\GameShop\GameShop\Program.cs:line 16
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

In the DB(same as in the object), Deleted field is not nullable.

In addition I might also say that I'm not using any automated code/table generators(like EF).

Please assist me with this issue, as I cannot find the solution myself.

update In another method I use following code and it executes without any issues:

_db.Companies.Any(x=>!x.Deleted && x.Name == textBoxCompanyName.Text)

update table in SQL was created like this:

CREATE TABLE [dbo].[Companies](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Deleted] [tinyint] NOT NULL,
[DateCreated] [datetime] NOT NULL,
[DateModified] [datetime] NULL,
[Name] [varchar](50) NOT NULL)

Also, when debugging, I noticed that SQL queryes, that LINQ creates, are odd:

SELECT [t0].[Id], ... FROM [Companies] as [t0]

Usually I see following:

SLECT [t0].[Id] as [Id], ... FROM [Companies] as [t0]

Maybe this will help in finding the solution.

7
  • How "deleted" field is defined in DB? Any chance it is nullable? Have you tried to just get values as string instead of bool and inspect once that may cause an error? Commented Jan 13, 2014 at 2:38
  • try List<Company> comp = _db.Companies.Where(x =>(x.Deleted !=null && !x.Deleted)).ToList(); Commented Jan 13, 2014 at 2:39
  • Please could you include your table definition? Also, might help if you include your dbml file. It looks like there is a mismatch between the mapped column to your property. Most likely it is mapped to an int or something stupid. Looking at the stack trace its easy to see that the SQL was generated, run, and it is the mapping to your entity classes that failed. Commented Jan 13, 2014 at 2:41
  • @Damith already tried: same error Commented Jan 13, 2014 at 3:14
  • 2
    @XpyM can you change [tinyint] column to [bit]? Commented Jan 13, 2014 at 3:32

1 Answer 1

6

Your DbMapping is incorrect. You need to change your [dbo].[Companies].[Deleted] to bit or change your Company.Deleted to byte.

ADO.Net converts tinyint to System.Byte (http://msdn.microsoft.com/en-us/library/cc716729%28v=vs.110%29.aspx).

In effect you are then trying to direct cast that byte to a bool. Which is where your exception comes from.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.