我有数据库与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