EF航海财产 [英] EF Navigational Property

查看:71
本文介绍了EF航海财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设Project和Employee是数据库实体,

如果我使用null项目创建Employee,则为
是否可以将属性导航到Projects列表> ;员工


例如。项目值列表将是 


(没有项目ID的员工)


ProjectId = Null, 


员工=(具有空项目ID的员工名单)







(具有项目ID的员工)


ProjectId = 1, 


员工=(具有项目ID的员工列表)


公共类项目
{
public virtual int ProjectId {get;组; }
public virtual ICollection< Employee>员工{get;组; }
}

公共类员工
{
public virtual int EmployeeId
public virtual Project Project {get;组; }
}


感谢您的帮助。






JohnM




解决方案

为了让你的延迟加载工作,你需要在Employee类型上声明一个int ProjectId。然后,您要么在Project属性上使用配置或ForeignKey属性将其链接到ProjectId。如果没有,那么EF将无法获得
如何加载导航属性。


根据您在Employee中定义ProjectId的方式,确定它是否可以为空。如果ProjectId是int,那么它不能为null。尝试将此类对象保存到数据库将导致保存为0,假设您的数据库受到适当约束,
将因约束错误而失败。如果员工不必参与项目,那么将ProjectId创建为int?而不是。


当您加载Employee时,如果设置了ProjectId,那么您可以引用Project属性以使其延迟加载(或使用Include自动加载它)。由于EF在上下文中缓存数据,因此如果您取消引用Employees,您将获得项目中
的员工列表。但是,如果没有设置Project,那么你将得到一个空引用异常,因为不会设置该属性。


另一方面,Project.Employees将触发所有员工的查询使用ProjectId设置。由于您要查询与项目关联的员工,因此每个员工的Project属性将设置为您要枚举的同一对象。这个
当然假设你已经使用属性或配置正确配置了导航属性。


Michael Taylor

http:// www。 michaeltaylorp3.net


Assuming that Project and Employee are DB entities,
If I created Employee with null project,
Is it possible to navigate property to list of Projects > Employees

E.g. List of Project value would be 

(Employees WITHOUT project id)

ProjectId = Null, 

Employees = (list of employees with Null Project Id)


(Employees WITH project id)

ProjectId = 1, 

Employees = (list of employees with Project Id)

        public class Project
        {
            public virtual int ProjectId { get; set; }
            public virtual ICollection<Employee> Employees { get; set; }
        }

        public class Employee
        {
            public virtual int EmployeeId
            public virtual Project Project { get; set; }
        }

Thanks for your help.


JohnM


解决方案

In order for your lazy loading to work you'd need to declare an int ProjectId on the Employee type. Then you'd either want to use a configuration or ForeignKey attribute on the Project property to link it to the ProjectId. Without that EF won't be able to figure out how to load the navigation property.

Depending upon how you define ProjectId in Employee determines whether it can be nullable or not. If ProjectId is an int then it cannot be null. Trying to save such an object to the DB will result in a 0 being saved which, assuming your DB is properly constrainted, will fail with a constraint error. If an employee does not have to be part of a project then create ProjectId as int? instead.

When you load the Employee, if ProjectId is set then you can reference the Project property to have it lazy loaded (or use Include to automatically load it). Since EF caches data it loads in the context, if you then dereference the Employees you'll get the list of employees in the project. However if Project is not set then you'll get a null reference exception as the property won't be set.

Going the other way, Project.Employees will trigger a query for all employee's with the ProjectId set. Since you're querying for employee's associated with the project, each employee's Project property will be set to the same object you're enumerating. This is assuming of course you've configured the navigation properties properly using attributes or a configuration.

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于EF航海财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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