【发布时间】:2011-08-03 18:05:38
【问题描述】:
我正在尝试了解定义我的 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 进行身份验证。代码中提到了这些问题(作为 cmets)。但为了澄清:
- 我应该在关系中定义对象并在每次需要时使用
.Include,还是最好存储对象的 ID 并在每次需要时尝试从该 ID 获取数据?在处理MembershipUser类而不是我定义的类时,我应该使用不同的方法吗? -
virtual除了启用延迟加载还有什么其他用途?我应该在哪里避免它,我应该在哪里使用它?
谢谢。
更新:我刚刚测试了用ublic virtual MembershipUser User 定义定义Employee。结果是我的表中添加了 4 列:
- 用户邮箱,
- 用户评论,
- User_IsApproved,
- User_LastLoginDate,
- User_LastActivityDate
没有用户独有的东西(User_Email 被定义为可为空)。因此,如果您想在您的课程中拥有用户,请为 MembershipUser 编写一个包装器,或者只存储 UserID。
谢谢拉迪斯拉夫和塞尔吉。
【问题讨论】:
标签: entity-framework .net-4.0 lazy-loading foreign-key-relationship entity-framework-4.1