所以我使用DataContext作爲連接到數據庫。我想從DB中獲得所有公司,但刪除的公司除外。 我的公司對象:C#Linq to SQL無效的轉換異常
[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>();
}
}
我的DataContext對象:
class DataManager : DataContext {
public Table<Company> Companies {
get {
return this.GetTable<Company>();
}
}
//other tables emited
}
當我想要得到的公司(除了已刪除),我用這個:
List<Company> comp = _db.Companies.Where(x => !x.Deleted).ToList();
_db是單獨創建並且在此時不爲NULL。
而我在「!x.Deleted」部分得到一個異常:「InvalidCastException:指定的轉換無效。」
堆棧跟蹤:
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()
在DB(相同的對象),刪除字段不可爲空。
另外我也可以說我沒有使用任何自動化的代碼/表生成器(如EF)。
請幫我解決這個問題,因爲我自己找不到解決方案。
更新 在另一種方法我用下面的代碼,它的執行沒有任何問題:
_db.Companies.Any(x=>!x.Deleted && x.Name == textBoxCompanyName.Text)
更新 表中的SQL是這樣創建的:
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)
而且,在調試時,我注意到LINQ創建的SQL查詢很奇怪:
SELECT [t0].[Id], ... FROM [Companies] as [t0]
通常我看到以下內容:
SLECT [t0].[Id] as [Id], ... FROM [Companies] as [t0]
也許這將在尋找解決方案的幫助。
如何在數據庫中定義「已刪除」字段?它有可能是空的嗎?你有沒有嘗試只是將值作爲'string'而不是'bool'並檢查一次,可能會導致錯誤? –
嘗試'List comp = _db.Companies.Where(x =>(x.Deleted!= null &&!x.Deleted))。ToList();' –
Damith
請問您能否包含您的表定義?另外,如果你包含你的dbml文件可能會有所幫助。看起來映射列與您的媒體資源之間存在不匹配。很可能它被映射到一個int或者一些愚蠢的東西。查看堆棧跟蹤,很容易看到SQL已生成,運行,它是映射到失敗的實體類。 – Aron