如何使用L2E或EF外键? [英] how to use Foreign key in L2E or EF?

查看:196
本文介绍了如何使用L2E或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屋!

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