asp.net-core-mvc – 在EF7中加载引用
发布时间:2020-12-15 20:38:43 所属栏目:asp.Net 来源:网络整理
导读:我有两个课程 – 作者和博客: public class Author{ public Author() { Blogposts = new HashSetBlogpost(); } public int Id { get; set; } public string Name { get; set; } public virtual ICollectionBlogpost Blogposts { get; set; }} 和 public cla
我有两个课程 – 作者和博客:
public class Author { public Author() { Blogposts = new HashSet<Blogpost>(); } public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Blogpost> Blogposts { get; set; } } 和 public class Blogpost { public Blogpost() { } // Properties public int Id { get; set; } public string Text { get; set; } public int AuthorId { get; set; } public Author Author { get; set; } } 使用EF7(beta4),我按以下方式连接它们: public partial class MyDbContext : DbContext { public virtual DbSet<Author> Author { get; set; } public virtual DbSet<Blogpost> Blogpost { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Author>(entity => { entity.Property(e => e.Id) .ForSqlServer().UseIdentity(); }); modelBuilder.Entity<Blogpost>(entity => { entity.Property(e => e.Id) .ForSqlServer().UseIdentity(); }); modelBuilder.Entity<Blogpost>(entity => { entity.Reference<Author>(d => d.Author).InverseCollection(p => p.Blogposts).ForeignKey(d => d.AuthorId); }); } } 当我访问博客帖子Db.Blogpost.First(x => x.Id == id)时,我检索了Blogpost对象 – 但是,.Author属性为null.此外,在检索任何Author对象时,它的.Blogposts集合为空. 我知道EF7既没有实现预先加载也没有延迟加载.但是,我如何检索/分配通过外键引用的任何对象? 解决方法
EF 7实施了预先加载.
使用.Include var post = context.Blogpost.First(); // post.Author will be null var post = context.Blogpost.Include(b => b.Author).First(); // post.Author will be loaded 有关使用集合的更多信息,请参阅此问题的答案:How to work with collections (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 从启用AJAX的WCF服务返回错误详细信息
- asp.net – “连接字符串指定本地Sql Server Express实例”
- asp.net – 如何在aspx页面中添加Paypal购买按钮
- asp.net-mvc – 我的表单应该在我的编辑器模板内部还是外部
- asp.net-mvc-4 – 缺少尾部斜杠的MVC错误行为的解决方法
- asp.net-mvc – 在MVC4中绑定的正确方法
- asp.net-mvc – ASP.NET MVC WebApi:没有为此对象定义的无
- ASP.NET知识点总结2
- 动手造轮子:实现一个简单的依赖注入(零)
- ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的
推荐文章
站长推荐
- ASP.NET ::在page_load期间,如何获取提交回发的控
- 在ASP.NET中使用?感到困惑
- asp.net-mvc-4 – 高级WebGrid教程或解决方案
- asp.net-mvc – ASP.NET MVC不提供默认文档
- asp.net-mvc – 更新实体框架MVC中的子实体
- asp.net-mvc – ASP.NET MVC 3确定会话状态(新的
- asp.net – CSS压缩和组合/ js缩小 – 在运行时或
- ASP.NET Core 下自定义权限验证
- ASP.NET DropDownList OnSelectedIndexChanged事
- Asp.net WebApi HttpClient强类型PostAync
热点阅读