Omu*_*Omu 6 entity-framework entity-framework-4.1
我有这门课
public class Comment
{
public long Id { get; set; }
public string Body { get; set; }
public long OwnerId { get; set; }
public virtual Account Owner { get; set; }
public DateTime CreationDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
问题是虚拟财产所有者是我null object reference exception在做的时候得到的:
comment.Owner.Name
Run Code Online (Sandbox Code Playgroud)
在保存对象之后(从同一个DbContext实例)调用此权限时,可以使用新的上下文
谁知道这个呢?
Lad*_*nka 18
那是因为你Comment用构造函数创建的.这意味着Comment实例未被代理,并且它不能使用延迟加载.您必须使用Create方法DbSet来获取代理实例Comment:
var comment = context.Comments.Create();
// fill comment
context.Comments.Add(comment);
context.SaveChanges();
string name = comment.Owner.Name; // Now it should work because comment instance is proxied
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1221 次 |
| 最近记录: |