我正在尝试理解定义POCO类的最佳方法,以便能够使用Entity Framework的代码优先功能。
我希望在我的类之间定义一些外键关系,包括用户之间以及类之间的关系。例如,考虑以下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?
}
用户是Membership用户,位于System.Web.Security中,正在通过FormsAuthentication或ActiveDirectoryAuthentication进行身份验证。问题在代码中已经提到(作为注释)。但是为了澄清:
- 我应该定义关系中的对象并在每次需要时使用.Include还是更好地存储对象的ID并且每次需要数据时尝试从该ID获取数据?处理MembershipUser类时是否应该使用不同的方法?
virtual
的其他用途是什么,除了启用延迟加载?在哪里应该避免使用它,在哪里应该使用它?
更新:我刚刚测试了使用
public virtual MembershipUser User
定义Employee的结果是我的表中添加了4个列:
- User_Email
- User_Comment
- User_IsApproved
- User_LastLoginDate
- User_LastActivityDate
MembershipUser
或仅存储UserID。
谢谢Ladislav和Sergi。