简体   繁体   English

实体框架7 RC1关系

[英]Entity Framework 7 RC1 relations

Now that I updated to ASP.NET 5 RC1, and Entity Framework 7 RC1, I expected to have relations enabled in my models. 现在,我已更新到ASP.NET 5 RC1和Entity Framework 7 RC1,我希望在模型中启用关系。 But I can't get this to work. 但是我无法使它正常工作。

Here are my models: 这是我的模型:

Post: 发布:

public class Post
{
    public int Id { get; set; }
    public string Text { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
}

Comment: 评论:

public class Comment
{
    public int Id { get; set; }
    public string Text { get; set; }

    public int PostId { get; set; }
    public virtual Post Post { get; set; }
}

I have inserted manually some data in the tables and tried to access Comments property of a post like this: 我已经在表中手动插入了一些数据,并尝试访问类似这样的帖子的Comments属性:

var post = context.Posts.Where(x => x.Id == 1).FirstOrDefault();

var sb = new StringBuilder();
foreach(var comment in post.Comments)
{
    sb.Append(comment.Text);
}

but the Comments property is always null. 但是Comments属性始终为null。 This post has two comments in table. 这篇文章在表中有两个评论。

What I'm doing wrong? 我做错了什么?

Entity Framework 7 doesn't support lazy loading (but it is on the roadmap ) so you need to Include your child relations: Entity Framework 7不支持延迟加载(但它在路线图上 ),因此您需要Include子关系:

var post = context
    .Posts
    .Include(p => p.Comments)
    .Where(x => x.Id == 1).FirstOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM