【发布时间】:2021-11-13 17:43:39
【问题描述】:
在使用带有 Entity Framework Core 的值对象内的实体时,我遇到了列命名问题。 EF 想要将值对象的实体的外键命名为长而丑陋的名称。例如。 PropertyName_ForeignKeyID。我想要的是使用 Fluent API 控制 db 列字段的名称。
如果我有一个名为 Measurement 的值对象,并且它具有一个名为 UnitOfMeasure 的实体的属性,则关系可以正常工作,但列命名非常难看。
以下是示例模型
public class EntityA
{
public int Id { get; set; }
public Measurement VO { get; set; }
}
public class Measurement
{
public double Value { get; set; }
public UnitOfMeasure UOM { get; set; }
}
// Second Entity
public class UnitOfMeasure
{
public int Id { get; set; }
public string Name { get; set; }
}
以及我尝试过的配置
var withEntity = modelBuilder.Entity<EntityA>().OwnsOne(t => t.VO);
// This works fine
withEntity.Property(e => e.Value).HasColumnName("WithEntityValue");
// Tried this but the results of the column name is VO_WithEntityValueUOMId
withEntity.HasOne(e => e.UOM).WithMany().HasForeignKey("WithEntityValueUOMId");
【问题讨论】: