2

我是第一个编码的新手,并且是从 DB Context 派生的。这是我的模型的摘录。

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

数据库创建工作正常,但我想使用 [Table("pm_ProductionStepLogMaterials")] 指定自动生成的多对多表“ProductionStepLogMaterials”的名称。

这可能吗?

4

2 回答 2

2

您应该protected override void OnModelCreating(DbModelBuilder modelBuilder)像这样覆盖您自己的 DBContext 类:

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");
     });
}
于 2013-04-03T09:44:35.433 回答
1

AFAIK,这对于数据注释是不可能的,但是使用配置流式 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"));
    }
于 2013-04-03T09:41:20.607 回答