如何使用L2E或EF外键? [英] how to use Foreign key in L2E or EF?
问题描述
我有一个用户
表里面有一个 PrivilegeId
外键指向特权
表,是有主键。
I have a User
table which has a PrivilegeId
foreign key points to a Privilege
table and is the primary key there.
在实体框架中,VS不会生成一个 PrivilegeId
在用户
为你的变量。它会生成一个权限
属性和 PrivilegeReference
属性你吧。
In Entity Framework, the VS will not generate a PrivilegeId
variable under User
for you. It will generate a Privilege
property and PrivilegeReference
property for you instead.
当我打开一个用户
的权限
属性是空
默认情况下。这意味着EF不会为你自动加载所指向实体。我想我可能做错了什么事?因为我对权限
当时没有信息,我无法加载权限
separatedly。我想EF应该加载所指向实体我,但我错过了一些东西。我需要的 PrivilegeId
我的用户
对象关联。
When I load a User
, the Privilege
property is null
by default. That means EF does not load refered entity for you automatically. I think I may did something wrong? I cannot load Privilege
separatedly because I have no info about the Privilege
at that time. I guess EF should load the refered entities for me but I missed something. I need the PrivilegeId
associated with my User
object.
任何人都可以帮助我
EDIT:
另一个答案: HTTP: //stackoverflow.com/questions/1262307/what-are-navigation-properties-in-entity-framework-for
推荐答案
听起来像是你想要的包括方法。
List<User> users = context.Users.Include("Privilege").ToList();
这篇关于如何使用L2E或EF外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!