简体   繁体   English

实体框架填写虚拟属性

[英]Entity framework fill in virtual property

I'm using EF 6 with code first, repository pattern + UOW pattern. 我先将EF 6和代码一起使用,即存储库模式+ UOW模式。

I have ca method that will return all items of a specific entity. 我有一个ca方法,它将返回特定实体的所有项目。

Ex: UnitOfWork.Customers.GetAllCustomers() that will return an IQueryable 例如: UnitOfWork.Customers.GetAllCustomers()将返回一个IQueryable

In the Customer model I have a virtual property called Adress that will be filled in at runtime 在客户模型中,我有一个称为Adress的虚拟属性,它将在运行时填充

public virtual Address Address { get; set; }

Whenever in my controller I call the GetAllCustomers, all information are there except the Address property that is null. 每当在我的控制器中,我调用GetAllCustomers时,除了Address属性为null之外,所有信息都在那里。

Any ideas how to return also the address ? 任何想法如何还返回地址?

See Entity Framework Loading Related Entities . 请参见实体框架加载相关实体

Either eager-load using .Include(c => c.Address) or enable lazy loading so EF will do the work for you. 可以使用.Include(c => c.Address)进行快速加载,也可以启用延迟加载,以便EF可以为您完成工作。

The Lazy Loading, load the entity only if Its accessed for the first time. 延迟加载仅在首次访问实体时才加载实体。 You should load it manually: 您应该手动加载它:

.Include(c => c.Address)

OR 要么

.Include("Address")

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

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