多个表的复杂模型(获取和插入)
我使用代码优先和实体框架。
我有一个 Registration
实体,它具有其他模型的多个属性:
public class Registration
{
public int ID { get; set; }
public int OrganizationID { get; set; }
public Address RegAddress { get; set; }
public ContactInformation RegContactInformation { get; set; }
public string Signature{ get; set; }
}
通过此设置,我确实有一个 Address
和 ContactInformation
模型。当我保存注册时,它会按我的预期工作。包含 3 个表的数据库(Registration
、Address
和 ContactInformation
)。 Registration
与其他两个具有 FK。
但是,当我尝试使用 EF 从数据库中获取注册时:
DBConnections dbConnections = new DBConnections();
var registrations = from r in dbConnections.PlayerRegistrations
where r.OrganizationID == orgID
select r;
Registration.Address 和 Registration.ContactInformation
为 null。我怎样才能做到这一点?
I am using code first and entity framework.
I have a Registration
entity which has several properties of other models:
public class Registration
{
public int ID { get; set; }
public int OrganizationID { get; set; }
public Address RegAddress { get; set; }
public ContactInformation RegContactInformation { get; set; }
public string Signature{ get; set; }
}
With this setup, I do have an Address
and ContactInformation
model. When I save a registration it works as I would expect. A database with 3 tables (Registration
, Address
, and ContactInformation
). With the Registration
having FK to the other two.
However when I try and get the registrations from my database using EF:
DBConnections dbConnections = new DBConnections();
var registrations = from r in dbConnections.PlayerRegistrations
where r.OrganizationID == orgID
select r;
The Registration.Address and Registration.ContactInformation
is null. How can I make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是正确的行为,因为 EF 本身从不加载相关实体。要加载相关属性,您必须使用以下方法之一:
延迟加载
延迟加载将为您提供相关实体的自动加载,但会生成对数据库的其他查询。当您第一次访问该属性时,将加载相关实体或相关集合。要使用延迟加载,您必须将实体中的所有导航属性标记为虚拟(并且不得禁用延迟加载或代理创建 - 默认情况下允许)。仅当用于加载主实体的上下文仍然存在时,延迟加载才有效。要允许延迟加载,您必须修改您的实体:
预加载
预加载将定义哪个关系必须与主实体一起加载。预加载是通过
Include
方法定义的。您可以将 Find 重写为:请注意,急切加载 对数据库返回的数据量和形式有很大影响。
显式加载
显式加载将允许您明确地说应该加载某些关系。您甚至可以定义一些加载相关实体的条件,这是其他两种方法无法实现的。您必须首先加载主实体,然后在处理上下文之前,您可以执行以下操作:
此方法对于加载相关集合更有用。
自定义加载
自定义加载意味着您将为每个关系使用单独的查询。它看起来像是您不想做的事情,但对于传输结果集的某些性能优化来说,这可能非常有用(这解决了急切加载部分中链接的问题)。此方法的要点是,如果您对关系使用单独的查询,EF 仍将正确填充导航属性。此方法仅对加载相关集合有意义,并且仅在关闭延迟加载时才有效。
That is correct behavior because EF never loads related entities itself. To load related properties you must use one of following approaches:
Lazy loading
Lazy loading will provide you automatic loading of related entities but it will generate additional queries to the database. Related entity or related collection will be loaded when you access the property for the first time. To use lazy loading you must mark all navigation properties in the entity as
virtual
(also lazy loading or proxy creation mustn't be disable - it is allowed by default). Lazy loading works only if the context used to load the main entity is still alive. To allow lazy loading you must modify your entity:Eager loading
Eager loading will define which relation must be load together with your main entity. Eager loading is defined by
Include
method. You can rewrite Find as:Be aware that eager loading has big impact on the amount and form of data returned from database.
Explicit loading
Explicit loading will allow you to explicitly saying that some relation should be loaded. You can even define some condition for loading related entities which is not possible with other two methods. You must first load the main entity and before disposing the context you can do something like:
This method is more useful for loading related collections.
Custom loading
Custom loading means that you will use separate query for each relation. It looks like something you don't want to do but for some performance optimization of transferred result sets this can be very useful (this solves problem linked in eager loading part). The point of this method is that if you use separate query for relation EF will still correctly fill navigation properties. This method makes sense only for loading related collections and it works only if lazy loading is turned off.