【问题标题】:Entity Framework Code-First - Defining relationships with MembershipUser实体框架代码优先 - 定义与 MembershipUser 的关系
【发布时间】: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


    【解决方案1】:

    我建议将外键声明为导航属性,这样您就可以随时直接访问相关属性,而不必自己从数据库中显式检索它(它将被延迟加载)。

    所以你的模型看起来像:

    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 课程还可以。

    需要明确的是,它会像您最初使用它一样工作,但您需要使用ForeignKeyAttribute 明确标记属性,如果您愿意放弃对如何在数据库中命名 FK 进行一点点控制。

    【讨论】:

    • 谢谢 Sergi,没有注意到 MembershipUser 没有默认构造函数。
    • @Kaymar - 不客气。顺便说一句,我是从头顶上说出来的,所以不要相信我的话。做检查! :)
    • @Kaymar - 刚刚检查过:它确实有一个默认构造函数,但它是protected,所以出于所有目的和意图,它没有;)
    【解决方案2】:

    如果首先是代码,我可能会使用这个:

    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。导航属性上的虚拟关键字用于延迟加载,其他映射属性上的虚拟关键字用于更改跟踪。

    【讨论】:

    • 谢谢拉迪斯拉夫。 EF 在 Employee 表中对 public virtual User User 的行为如何? EF 是否将其定义为复杂类型?
    • 是复杂类型还是实体?
    • 它是一个MembershipUser,在.NET 中的System.Web.Security 命名空间中定义
    • @Ladislav:我试过了,结果是我的表中有 4 行。我会更新问题...
    • @Ladislav:谢谢!通过阅读您的帖子,我学到了很多新东西。
    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2012-10-11
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    相关资源
    最近更新 更多