Code First EF 关系重复

发布于 2024-12-29 04:31:13 字数 1497 浏览 0 评论 0原文

我有一个工作模型,但注意到该关系已在数据库中创建了两次。最初,它在表中创建了两列,但通过添加指定的外键属性,现在只有一列。

我有一个 Account 类,其中有许多可以使用该帐户的雇主。 (一对多)以下是类:

public class Account
{
    public int AccountId { get; set; }

    [Required(ErrorMessage = Constants.ValidationMessages.FieldRequired)]
    public string  Name { get; set; }

    public int? PrimaryUserId { get; set; }
    [ForeignKey("PrimaryUserId")]
    public Employer PrimaryUser { get; set; }

    [ForeignKey("EmpAccountId")]
    public ICollection<Employer> Employers { get; set; }

}

这是继承的 Employer 类

public class Employer : User
{
    public Employer()
    {
        DepartmentsToPost = new Collection<Department>();
        Contacts = new Collection<Contact>();
    }

    [Display(Name = "Workplaces to advertise jobs")]
    public virtual ICollection<Department> DepartmentsToPost { get; set; }

    public int EmpAccountId { get; set; }
    [ForeignKey("EmpAccountId")]
    public virtual Account Account { get; set; }

    public override string UserType
    {
        get { return "Employer"; }
    }
}

用户表:

UserId
Username
FirstName     
Surname 
EmpAccountId
Discriminator 

帐户表

AccountId
Name
PrimaryUserId

有一个返回用户表的链接 - 这是针对 PrimaryUser 字段的,这是正确的。还有另外两种关系:Account -> Account雇主。 EF 将它们命名为 Account_EmployersEmployer_Account。这些是重复的。

我怎样才能防止这种情况发生?

I have a working model, but have noticed that the relationship has been created twice in the database. Originally, it created two columns in the table, but with the addition of a specified foreign key attribute it has now just the one.

I have an Account class, which has many employers who can use the account. (one to many) Here are the classes:

public class Account
{
    public int AccountId { get; set; }

    [Required(ErrorMessage = Constants.ValidationMessages.FieldRequired)]
    public string  Name { get; set; }

    public int? PrimaryUserId { get; set; }
    [ForeignKey("PrimaryUserId")]
    public Employer PrimaryUser { get; set; }

    [ForeignKey("EmpAccountId")]
    public ICollection<Employer> Employers { get; set; }

}

here is the inherited Employer class

public class Employer : User
{
    public Employer()
    {
        DepartmentsToPost = new Collection<Department>();
        Contacts = new Collection<Contact>();
    }

    [Display(Name = "Workplaces to advertise jobs")]
    public virtual ICollection<Department> DepartmentsToPost { get; set; }

    public int EmpAccountId { get; set; }
    [ForeignKey("EmpAccountId")]
    public virtual Account Account { get; set; }

    public override string UserType
    {
        get { return "Employer"; }
    }
}

User Table:

UserId
Username
FirstName     
Surname 
EmpAccountId
Discriminator 

Account Table

AccountId
Name
PrimaryUserId

There is one link back to the User table - this is for the PrimaryUser field, and this is correct. There are two other relationships: Account -> Employers. EF has named them Account_Employers and Employer_Account. These are duplicates.

How can I prevent this occuring?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2025-01-05 04:31:13

Employers 集合应使用 InversePropertyAttribute 指向另一侧的导航属性。

public class Account
{
    public int AccountId { get; set; }

    [Required(ErrorMessage = Constants.ValidationMessages.FieldRequired)]
    public string  Name { get; set; }

    public int? PrimaryUserId { get; set; }
    [ForeignKey("PrimaryUserId")]
    public Employer PrimaryUser { get; set; }

    [InverseProperty("Account")]
    public ICollection<Employer> Employers { get; set; }    
}

The Employers collection should be decorated with InversePropertyAttribute to point to the navigational property on the other side.

public class Account
{
    public int AccountId { get; set; }

    [Required(ErrorMessage = Constants.ValidationMessages.FieldRequired)]
    public string  Name { get; set; }

    public int? PrimaryUserId { get; set; }
    [ForeignKey("PrimaryUserId")]
    public Employer PrimaryUser { get; set; }

    [InverseProperty("Account")]
    public ICollection<Employer> Employers { get; set; }    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文