在实体框架对象属性NULL值 [英] NULL values in object properties in Entity Framework

查看:182
本文介绍了在实体框架对象属性NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表:文章作者注释( 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.

这篇关于在实体框架对象属性NULL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆