简体   繁体   English

EF6 生成奇怪的外键

[英]EF6 generates weird foreign keys

I am having a little problem using EF 6. These are my models (well, I am omitting the non-relevant properties):我在使用 EF 6 时遇到了一点问题。这些是我的模型(好吧,我省略了不相关的属性):

[Table("Departments")]
public class Department
{
    public string Id { get; set; }

    public string Name { get; set; }
    public string Description { get; set; }

    public IList<Staff> Staff { get; set; }

    public Staff HOD { get; set; }
}

[Table("Staff")]
public class Staff
{
    [Key]
    public string EmployeeId { get; set; }

    public string Name { get; set; }

    public Department Department { get; set; }
}

This is the migration that EF6 is generating (well, just the create methods):这是 EF6 正在生成的迁移(嗯,只是创建方法):

CreateTable(
    "dbo.Staff",
    c => new
        {
            EmployeeId = c.String(nullable: false, maxLength: 128),
            Name = c.String(),
            Department_Id = c.String(maxLength: 128),
            Department_Id1 = c.String(maxLength: 128),
        })
    .PrimaryKey(t => t.EmployeeId)
    .ForeignKey("dbo.Departments", t => t.Department_Id)
    .ForeignKey("dbo.Departments", t => t.Department_Id1)
    .Index(t => t.Department_Id)      //what's this?
    .Index(t => t.Department_Id1);    //what's this?

CreateTable(
    "dbo.Departments",
    c => new
        {
            Id = c.String(nullable: false, maxLength: 128),
            Name = c.String(),
            Description = c.String(),
            HOD_EmployeeId = c.String(maxLength: 128),
        })
    .PrimaryKey(t => t.Id)
    .ForeignKey("dbo.Staff", t => t.HOD_EmployeeId)
    .Index(t => t.HOD_EmployeeId);

Take a look at the foreign keys generated.看看生成的外键。 Something seems to be amiss.似乎有些不对劲。 How do I correct this?我该如何纠正?

try to add some relations尝试添加一些关系

[Table("Departments")]
public class Department
{
    public string Id { get; set; }

    public string Name { get; set; }
    public string Description { get; set; }

    public virtual IList<Staff> Staff { get; set; }

     public  int HodId {get set;}

     [ForeignKey("HodId")]
    public Staff Hod { get; set; }
}

[Table("Staff")]
public class Staff
{
    [Key]
    public string EmployeeId { get; set; }

    public string Name { get; set; }

    public int DepartmentId { get; set; }

    public virtual Department Department { get; set; }
}

I had to use Fluent API to make this relationship work:我不得不使用 Fluent API 来使这种关系起作用:

modelBuilder.Entity<Staff>()
    .HasRequired<Department>(staff => staff.Department)
    .WithMany(department => department.Staff)
    .HasForeignKey<string>(staff => staff.DepartmentId);

Changes to the models:模型的变化:

public Department Department { get; set; }

changed to变成

public string DepartmentId { get; set; }
public virtual Department Department { get; set; }

and

public IList<Staff> Staff { get; set; }

public Staff HOD { get; set; }

changed to变成

public virtual IList<Staff> Staff { get; set; }

public Staff HOD { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM