C# 编码表的第一个多对多集合名称

C# 编码表的第一个多对多集合名称,c#,ef-code-first,C#,Ef Code First,我是第一个从DB上下文派生代码的新手。这是我的模型的摘录 [Table("pm_Material")] public class Material { public Material() { this.ProductionStepLogs = new HashSet<ProductionStepLog>(); } [Key] public int MaterialId { get; set; } public int

我是第一个从DB上下文派生代码的新手。这是我的模型的摘录

[Table("pm_Material")]
public class Material
{
    public Material()
    {
        this.ProductionStepLogs = new HashSet<ProductionStepLog>();
    }

    [Key]
    public int MaterialId { get; set; }
    public int MaterialTypeId { get; set; }
    public string Description { get; set; }
    public decimal CostRate { get; set; }

    public virtual MaterialType MaterialType { get; set; }
    public virtual ICollection<ProductionStepLog> ProductionStepLogs { get; set; }
}

[Table("pm_ProductionStepLog")]
public class ProductionStepLog
{
    public ProductionStepLog()
    {
        this.Materials = new HashSet<Material>();
    }

    [Key]
    public System.Guid ProductionStepLogId { get; set; }
    public int ProductionStepId { get; set; }
    public System.Guid ProductId { get; set; }
    public Nullable<System.DateTime> BeginStep { get; set; }
    public Nullable<System.DateTime> EndStep { get; set; }
    public int UserId { get; set; }

    public virtual Product Product { get; set; }
    public virtual ProductionStep ProductionStep { get; set; }
    public virtual ICollection<Material> Materials { get; set; }
}
[表(“pm_材料”)]
公开课材料
{
公共材料()
{
this.ProductionStepLogs=new HashSet();
}
[关键]
公共int MaterialId{get;set;}
public int MaterialTypeId{get;set;}
公共字符串说明{get;set;}
公共十进制成本率{get;set;}
公共虚拟MaterialType MaterialType{get;set;}
公共虚拟ICollection ProductionStepLogs{get;set;}
}
[表(“pm_生产步骤日志”)]
公共类ProductionStepLog
{
公共产品步骤日志()
{
this.Materials=new HashSet();
}
[关键]
public System.Guid ProductionStepLogId{get;set;}
public int ProductionStepId{get;set;}
public System.Guid ProductId{get;set;}
公共可为空的BeginStep{get;set;}
公共可为null的结束步骤{get;set;}
public int UserId{get;set;}
公共虚拟产品产品{get;set;}
公共虚拟产品步骤产品步骤{get;set;}
公共虚拟ICollection材质{get;set;}
}
DB创建工作正常,但我想使用[table(“pm_ProductionStepLogMaterials”)]指定自动生成的多对多表“ProductionStepLogMaterials”的名称


这可能吗

AFAIK,这在数据注释中是不可能的,但在配置fluent API中是可能的:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<E1Type>()
            .HasMany(e1 => e1.Collection)
            .WithMany(e2 => e2.Collection)
            .Map(config => config.ToTable("MyTable"));
    }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity()
.HasMany(e1=>e1.Collection)
.WithMany(e2=>e2.Collection)
.Map(config=>config.ToTable(“MyTable”);
}

您应该在自己的DBContext类的ModelCreating(DbModelBuilder modelBuilder)上重写受保护的override void,如下所示:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{    
   modelBuilder.Entity<Material>()
     .HasMany(a => a.ProductionStepLog)
     .WithMany(a => a.Material)
     .Map(x =>
     {
        x.ToTable("NameOfYourTable");
        x.MapLeftKey("MaterialId");
        x.MapRightKey("ProductionStepLogId");
     });
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{    
modelBuilder.Entity()
.HasMany(a=>a.ProductionStepLog)
.WithMany(a=>a.Material)
.Map(x=>
{
x、 ToTable(“您的表格名称”);
x、 MapLeftKey(“物质化”);
x、 MapRightKey(“ProductionStepLogId”);
});
}

这对你有帮助吗?@NSGaga,是的,只是名字。@MikroDel,我会测试它并做出回应。
DataAnnotations
也能做到吗?@e.campver-我想没有-因为
DataAnnotations
是整个
实体框架
功能的一个子集。至于今天,您可以对最常见的场景使用
DataAnnotations
,但不能对所有场景都使用