gpt4 book ai didi

c# - 代码首先多对多设置表的名称

转载 作者:行者123 更新时间:2023-11-30 22:19:02 25 4
gpt4 key购买 nike

我刚开始接触代码并从 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”的名称。

这可能吗?

最佳答案

您应该覆盖您自己的 DBContext 类的 protected override void OnModelCreating(DbModelBuilder modelBuilder),如下所示:

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

关于c# - 代码首先多对多设置表的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15783497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com