6

我想了解定义我的POCO类能够使用实体框架代码优先功能的最佳方法。
我想在我的类中,用户之间以及类之间定义一些外键关系。例如,考虑以下3类:实体框架代码优先 - 定义与MembershipUser的关系

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除了启用延迟加载之外还有什么其他用途?我应该在哪里避免它,我应该在哪里使用它?

谢谢。

UPDATE:我刚刚测试过定义员工的ublic virtual MembershipUser User定义。其结果是加4列在我的表:

  • USER_EMAIL,
  • USER_COMMENT,
  • User_IsApproved,
  • User_LastLoginDate,
  • User_LastActivityDate

没有什么独特的用户(USER_EMAIL被定义为可空)。所以,如果你想让你的用户在你的课堂上,写一个MembershipUser的包装或只是存储用户ID。
谢谢Ladislav和Sergi。

回答

5

我会建议声明外键作为导航属性,这样你可以直接访问相关属性,而不必从数据库中明确地从数据库中明确地检索它(它将被延迟加载)。

所以,你的模型将如下所示:

public class Resume 
{ 
    public int ID {get; set;} 
    // or should it be: public virtual Employee EmployeePerson? 
    // --> yep, easier for you, innit? 
    public Employee Employee {get; set;} 

    // or should it be: public virtual Job UserJob? --> yep :) 
    public virtual Job Job {get; set;} 

    public DateTime EmploymentDate {get; set;} 
} 

public class Employee 
{ 
    public int EmployeeID {get; set;} 

    // or should it be: public virtual MembershipUser User? 
    // --> yes, as an approach, but read on for clarification. 
    public virtual MembershipUser User {get; set;} 

    // Is this correct at all? ---> needs to be declared as virtual 
    public virtual ICollection<Resume> Resumes {get; set;} 
} 

Job类是OK,据我可以告诉。

要清楚,它可以像你最初一样使用它,但是你需要明确地标记ForeignKeyAttribute的属性,如果你愿意放弃一个微小的控制FK在数据库中的命名方式。有关MembershipUser导航属性

+0

谢谢Sergi,没有注意到MembershipUser没有默认的构造函数。 – Kamyar 2011-04-12 12:14:57

+0

@Kaymar - 不客气。顺便说一句,我说我的头顶,所以不要听我的话。检查! :) – 2011-04-12 12:26:22

+1

@Kaymar - 刚刚检查:它确实有一个默认的构造函数,但它是'protected',所以对于所有目的和意图,它没有;) – 2011-04-12 12:27:52

3

在代码情况下,首先,我可能会使用这样的:

public class Job 
{ 
    public virtual int JobId {get; set;} 
    public virtual string JobTitle {get; set;} 
    public virtual ICollection<Resume> Resumes {get; set;} 
} 

public class Resume 
{ 
    [Key, Column(Order = 0)] 
    public virtual int EmployeeId {get; set;} 
    [Key, Column(Order = 1)] 
    public virtual int JobId {get; set;} 

    public virtual DateTime EmploymentDate {get; set;} 
    public virtual Employee Employee {get; set;} 
    public virtual Job Job {get; set;} 
} 

public class Employee 
{ 
    public virtual int EmployeeId {get; set;} 
    public virtual int UserId {ger; set;} 
    public virtual User User {get;set;} 
    public virtual ICollection<Resume> Resumes {get; set;} 
} 

实体中的外键是不好的,但它们在EF中使事情变得更容易。如果您不使用外键属性,EF将定义different type of relationship。导航属性上的虚拟关键字用于延迟加载,其他映射属性上的虚拟关键字用于更改跟踪。

+0

谢谢拉迪斯拉夫。 EF如何在Employee表中关于“公共虚拟用户用户”行为? EF是否将其定义为复杂类型? – Kamyar 2011-04-13 04:58:26

+0

它是一个复杂的类型或实体吗? – 2011-04-13 06:51:36

+0

它是一个'MembershipUser',它在.NET中定义在'System.Web.Security'命名空间 – Kamyar 2011-04-13 07:11:20