在实体框架对象属性NULL值实体、框架、属性、对象

2023-09-06 08:58:50 作者:最有故事的 一听就很有故事的

表:文章作者注释( 1条和1个作者可以有*评论)

Tables: Article, Author, Comment (1 article and 1 author can have * comments)

在数据库是1条,1作家和1条评论。

In the database is 1 article, 1 author and 1 comment.

的问题是,code

 myBD my_bd = new myBD();
 var articles = by_bd.Article;

工程确定,我可以看到一个作者文章有1条评论。

但是,code

    var comm = (from u in my_bd.Comment
                     where ......
                     select u);

返回发表评论,但它在性能文章作者 NULL值。为什么呢?

returns the comment but it has NULL values in property Article and Author. Why ?

推荐答案

实体框架不支持延迟加载(还),是悲观的默认。为了获得链接对象的集合,你必须明确地包括他们在您的查询。

Entity Framework does not support Lazy Loading (yet) and is pessimistic by default. In order to get linked object as collections you have to include them in your query explicitly.

var comm = from u in my_bd.Comment.Include("Article").Include("Author")
           where ......
           select u;

这样做,你明确地告诉EF做时,它创建查询联接。现在,你应该能够选择那些属性。

By doing this you are explicitly telling EF to do the joins when it creates the query. Now you should be able to select those properties.

 
精彩推荐