我有數據庫與Linq-To-Sql類映射。我試着對它執行一些基本的查詢,當我嘗試訪問子表時,它會失敗並返回InvalidCastException。Linq-to-SQL檢索子表拋出InvalidCastException
var query = (from p in PBZGdb.Instance.AuthenticationDatas
where p.Username == "Misha" && p.Password == "123"
select new { p.UserAccount }).AsEnumerable();
UserAccount UA = query.ElementAt(0).UserAccount; //this works!
int count = UA.Characters.Count(); //throws InvalidCastException here
和
var query = (from p in PBZGdb.Instance.AuthenticationDatas
where p.Username == "Misha" && p.Password == "123"
select new
{
p.UserAccount,
p.UserAccount.Characters
}) .AsEnumerable();
UserAccount UA = query.ElementAt(0).UserAccount; //throws InvalidCastException here
如果我只是試圖繼續異常後,幾秒鐘後,暫停該應用程序,它「檢索」中的數據,並進行沒有問題。有沒有人遇到過這樣的事情?我如何解決它?
p.s.字符表通過外鍵鏈接到UserAccounts。一個UserAccount可以有多個字符。
你使用什麼提供者?你在輸出中看到* real * SQL查詢嗎? – Tigran 2012-01-03 10:19:20
我不確定我們在這裏有足夠的信息;你可以訪問一個單獨的'Character'記錄,即'var char = db.Characters.First()'?我不知道這是不是一個簡單的列錯誤配置(長vs int,或者是一個狡猾的枚舉值等) – 2012-01-03 10:24:59
不相關:'p.Password'設置的鬧鐘聲音....應該是一個鹽味散列,真的 – 2012-01-03 10:25:30