gxc*_*rke 8 many-to-many code-first entity-framework-4
鉴于以下POCO课程:
public class Certification {
public int Id { get; set; }
public virtual ICollection<Employee> CertifiedEmployees { get; set; }
}
public class Employee {
public int Id { get; set; }
public virtual ICollection<Certification> Certifications { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用EF4 CTP4代码第一种方法创建数据库模型会创建所需的联结表:
CREATE TABLE [dbo].[Certifications_CertifiedEmployees](
[Certifications_Id] [int] NOT NULL,
[CertifiedEmployees_Id] [int] NOT NULL,
...
Run Code Online (Sandbox Code Playgroud)
但是,表名和列名称并不理想,因为它们是从关联的类属性名称生成的.我宁愿:
CREATE TABLE [dbo].[Employees_Certifications](
[Employee_Id] [int] NOT NULL,
[Certification_Id] [int] NOT NULL,
...
Run Code Online (Sandbox Code Playgroud)
有谁知道在这种情况下是否可以更改生成的列名称,还可以选择更改表名,以便Employees在Certifications之前?
谢谢,加里
任何人现在绊倒并使用较新版本的EF可能需要将最后一节@gxclarke的答案更改为:
.Map(
m => {
m.MapLeftKey("Employee_Id");
m.MapRightKey("Certification_Id");
m.ToTable("Employees_Certifications");
}
);
Run Code Online (Sandbox Code Playgroud)
因为看起来方法参数已更改为仅接受操作而不是表名.
我使用了流畅的API来修改生成的联结表:
modelBuilder.Entity<Employee>()
.HasMany(e => e.Certifications)
.WithMany(c => c.Employees)
.Map("Employees_Certifications", (e, c) => new {
Employee_Id = e.Id,
Certification_Id = c.Id });
Run Code Online (Sandbox Code Playgroud)