我的 LinqToSql 模型有一个非常有趣的问题。在我的一些表上,我有对其他表的引用,在 LinqToSql 中,这由 EnitiyRef 类表示,当您尝试访问引用表时,LinqToSql 将从数据库加载引用。
在我的开发机器上一切正常(引用完美加载)但昨晚我将更改上传到我们的生产服务器并在尝试访问我的表上的引用时开始出现 NullReferenceExceptions。
示例代码:
var sale = db.Sales.Single(s => s.ID == 1);
string username = sale.User.Name; // User is a reference to a User table
// LinqToSql will automatically load the
// row and access the fields i need.
// On my server the sale.User throws an exception that its null (User) but the user
// is definitly in the database (there is even a FK constraint from Sale to User)
起初我以为我的 DataContext 得到了 GC,但我仔细检查了所有内容但没有结果(除了它在我的盒子上工作)。
(服务器和我的盒子上的一切都是一样的,相同的 dll,相同的数据库模式等...) (我实际上将整个 DBF 文件复制到我的服务器,所以它的模式完全相同)
最佳答案
您是否打开了上下文日志并将开发箱中的结果与生产箱中的结果进行了比较?
关于c# - LinqToSql 引用的实体将抛出 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/182683/