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.
stringinstead ofbooland inspect once that may cause an error?List<Company> comp = _db.Companies.Where(x =>(x.Deleted !=null && !x.Deleted)).ToList();[tinyint]column to[bit]?