实体框架代码优先 - 定义与 MembershipUser 的关系
我试图了解定义 POCO 类的最佳方法,以便能够使用实体框架代码优先功能。
我想在我的类中、用户之间以及类本身之间定义一些外键关系。例如,考虑以下 3 个类:
Public class Job
{
public int JobID {get; set;}
public string JobTitle {get; set;}
public virtual ICollection<Resume> Resumes {get; set;} // Is this correct at all? How to access all resumes for a certain job? (many-to-many relationship between Job and Employee)
}
Public class Resume
{
public int EmployeeID {get; set;} // or should it be: public virtual Employee EmployeePerson?
public int JobID {get; set;} // or should it be: public virtual Job UserJob?
public DateTime EmploymentDate {get; set;}
}
public class Employee
{
public int EmployeeID {get; set;}
public int UserID{ger; set;} // or should it be: public virtual MembershipUser User?
public ICollection<Resume> Resumes {get; set;} // Is this correct at all?
}
用户是 System.Web.Security 中的会员用户,正在通过 FormsAuthentication 或 ActiveDirectoryAuthentication 进行身份验证。这些问题在代码中提到(作为注释)。但为了澄清:
- 我应该在关系中定义对象并在每次需要它们时使用
.Include
还是最好存储对象的 ID 并在每次需要时尝试从该 ID 获取数据需要吗?在处理MembershipUser
类而不是我定义的类时,我应该使用不同的方法吗? - 除了启用延迟加载之外,
virtual
还有什么用途?我应该在哪里避免它,在哪里应该使用它?
谢谢。
更新:我刚刚测试了使用公共虚拟MembershipUser User
定义来定义Employee。结果是我的表中添加了 4 列:
- User_Email、
- User_Comment、
- User_IsApproved、
- User_LastLoginDate、
- User_LastActivityDate
没有任何用户特有的列(User_Email 被定义为可为空)。因此,如果您想在类中包含用户,请为 MembershipUser
编写一个包装器或仅存储 UserID。
谢谢拉迪斯拉夫和塞尔吉。
I'm trying to understand the best way to define my POCO classes to be able to use Entity Framework code-first feature.
I want to define some foreign key relations in my classes, between the user and also between the classes themselves. For example consider the following 3 classes:
Public class Job
{
public int JobID {get; set;}
public string JobTitle {get; set;}
public virtual ICollection<Resume> Resumes {get; set;} // Is this correct at all? How to access all resumes for a certain job? (many-to-many relationship between Job and Employee)
}
Public class Resume
{
public int EmployeeID {get; set;} // or should it be: public virtual Employee EmployeePerson?
public int JobID {get; set;} // or should it be: public virtual Job UserJob?
public DateTime EmploymentDate {get; set;}
}
public class Employee
{
public int EmployeeID {get; set;}
public int UserID{ger; set;} // or should it be: public virtual MembershipUser User?
public ICollection<Resume> Resumes {get; set;} // Is this correct at all?
}
The user is a Membership user which is in System.Web.Security
which is being authenticated by FormsAuthentication or ActiveDirectoryAuthentication. The questions are mentioned in the code (as comments). But for clarification:
- Should I define the objects in relations and use
.Include
every time I need them or is it better to store the ID of the objects and try to get the data from that ID every time I need to? Should I use a different approach when dealing withMembershipUser
class instead of my defined classes? - What's other uses of
virtual
apart from enabling lazy loading? Where should I avoid it and where should I use it?
Thank you.
UPDATE: I have just tested to define Employee with the ublic virtual MembershipUser User
definition. The result was 4 added columns in my table:
- User_Email,
- User_Comment,
- User_IsApproved,
- User_LastLoginDate,
- User_LastActivityDate
Nothing unique to the user (User_Email is defined as nullable). So If you want to have user in you classes, write a wrapper for MembershipUser
or just store UserID.
Thank you Ladislav and Sergi.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议将外键声明为导航属性,这样您始终可以直接访问相关属性,而不必自己从数据库中显式检索它(它将被延迟加载)。
所以你的模型看起来像:
据我所知,你的 Job 类没问题。
需要明确的是,它的使用效果与您最初使用它时一样好,但是您需要使用
ForeignKeyAttribute
显式标记属性,如果您愿意的话,则不需要这样做对 FK 在数据库中的命名方式进行一点控制。关于
MembershipUser
导航属性的一点评论:恐怕您需要为MembershipUser
类编写一个包装器,因为据我所知,它不需要没有默认构造函数,因此 EF 可能无法在延迟加载时为您实例化它。关于将属性标记为
虚拟
的效果,您可能想看看这个问题:virtual 关键字在 Entity Framework 4.1 POCO Code First 中可以产生什么效果?I would recommend declaring the Foreign Keys as navigation properties, that way you can always access the related property directly without having to retrieve it explicitly from the database yourself (it will be lazy loaded).
So your model would look like:
Your
Job
class is OK as far as I can tell.To be clear, it will work as well using it as you had it initially, but you'll need to explicitly mark the properties with the
ForeignKeyAttribute
, which isn't necessary if you're willing to give up a tiny bit of control on how FK's are named in your database.One remark about the
MembershipUser
navigation property: I'm afraid you'll need to write a wrapper for theMembershipUser
class because as far as I can recall it doesn't have a default contructor and so EF will probably not be able to instantiate it for you on lazy loading.About the effects of marking a property as
virtual
, you might want to take a look at this question: What effect(s) can the virtual keyword have in Entity Framework 4.1 POCO Code First?如果首先使用代码,我可能会使用这个:
实体中的外键很糟糕,但它们使 EF 中的事情变得更容易。如果不使用外键属性 EF 将定义 不同类型的关系。导航属性上的虚拟关键字用于延迟加载,其他映射属性上的虚拟关键字用于更改跟踪。
In case of code first I would probably use this:
Foreign keys in entity are bad but they make things much easier in EF. If you don't use foreign key property EF will define different type of relationship. Virtual keyword on navigation property is for lazy loading and virtual keyword on other mapped properties is for change tracking.