实体框架:加载多对一实体 [英] Entity Framework: Loading Many to One entity

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

问题描述

说我有一个角色实体和网站的实体。目前已经有很多角色,一个网站,所以在角色类存在着重新presents这种关系站点属性。如果我想要的角色,一个网站,我会做这样的:

Say I have a role entity and a site entity. Now there are many roles to one site, so on the Role class there is a Site property that represents that relationship. If I wanted the roles for a site I would do this:

Site.Roles.Load()

问题是,由于在角色类的网站属性不是一个集合,但只是一个单一的实体,没有Load方法:

Problems is, since the Site property on the Role class isn't a collection but just a single entity, there is no Load method:

currentRole.Site //????

这样一个角色被加载时,该网站为空,也没有办法让网站不如说是对角色集合的查询,以获得SITEID,从网站收集得到的网站,并最终设定其他到currentRole的站点属性。

So that when a role is loaded, the Site is null and there is no way to get the site other than say a query on the role collection to get the SiteID, getting the Site from the site collection, and finally setting that to currentRole's site property.

必须有一个更好的办法?我一定要逼一些参与查询的?看起来这将由code生成就像Load方法的行为。

There has to be a better way? Do I have to force some kind of join in the query? Seems like this would be generated by code just like the Load method behaves.

推荐答案

访问,则会加载它。如果你想让它加载它明确应该在对象查询的.INCLUDE()方法。

Accessing it will load it. If you want it to load it explicitly there should be an .Include() method on the object query.

db.Sites.Include("Role").ToList();

下面是一些文档

这篇关于实体框架:加载多对一实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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