开发者

Naming conventions in generated many-to-many table using EF4 CTP4 code first approach

开发者 https://www.devze.com 2023-01-11 07:19 出处:网络
Given the following POCO classes: public class Certification { public int Id { get; set; } public virtual ICollection<Employee> CertifiedEmployees { get; set; }

Given the following POCO classes:

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; }
}

Creating the database model using the EF4 CTP4 code first approach does create the desired junction table:

CREATE TABLE [dbo].[Certifications_CertifiedEmployees](
[Certifications_Id] [int] NOT NULL,
[CertifiedEmployees_Id] [int] NOT NULL,
    ...

However, the table name and the column names are not ideal because they are generated from the associated class property names. I would rather 开发者_JAVA技巧have:

CREATE TABLE [dbo].[Employees_Certifications](
[Employee_Id] [int] NOT NULL,
[Certification_Id] [int] NOT NULL,
    ...

Does anyone know if it is possible to change the generated column names in this scenario, and optionally to also change the table name so that Employees is before Certifications?

Thanks, Gary


Anyone stumbling over this now and using a newer version of EF might need to change the last section @gxclarke answer to:

  .Map(
      m => {
          m.MapLeftKey("Employee_Id");
          m.MapRightKey("Certification_Id");
          m.ToTable("Employees_Certifications");
      }
   );

As it looks as though the method parameters have changed to only accept the action and not the table name.


I used the fluent API to modify the generated junction table:

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 });
0

精彩评论

暂无评论...
验证码 换一张
取 消