EF6 生成奇怪的外键
EF6 generates weird foreign keys
我在使用 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; }
}
这是 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);
看看生成的外键。似乎有些不对劲。我该如何纠正?
尝试添加一些关系
[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; }
}
我必须使用 Fluent API 才能使这种关系有效:
modelBuilder.Entity<Staff>()
.HasRequired<Department>(staff => staff.Department)
.WithMany(department => department.Staff)
.HasForeignKey<string>(staff => staff.DepartmentId);
模型变化:
public Department Department { get; set; }
改为
public string DepartmentId { get; set; }
public virtual Department Department { get; set; }
和
public IList<Staff> Staff { get; set; }
public Staff HOD { get; set; }
改为
public virtual IList<Staff> Staff { get; set; }
public Staff HOD { get; set; }
我在使用 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; }
}
这是 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);
看看生成的外键。似乎有些不对劲。我该如何纠正?
尝试添加一些关系
[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; }
}
我必须使用 Fluent API 才能使这种关系有效:
modelBuilder.Entity<Staff>()
.HasRequired<Department>(staff => staff.Department)
.WithMany(department => department.Staff)
.HasForeignKey<string>(staff => staff.DepartmentId);
模型变化:
public Department Department { get; set; }
改为
public string DepartmentId { get; set; }
public virtual Department Department { get; set; }
和
public IList<Staff> Staff { get; set; }
public Staff HOD { get; set; }
改为
public virtual IList<Staff> Staff { get; set; }
public Staff HOD { get; set; }