[英]Entity Framework Core 2 Owned Entity Types - Change Names of Table Columns
I have entity classes, something like this:我有实体类,如下所示:
public class Bike
{
public int Id { get; set; }
public int ModelId { get; set; }
public Model Model { get; set; }
public Contact Contact { get; set; }
}
[Owned]
public class Contact
{
[Required]
[StringLength(255)]
public string Name { get; set; }
[StringLength(255)]
public string Email { get; set; }
[Required]
[StringLength(255)]
public string Phone { get; set; }
}
It will generate table by default:它会默认生成表:
migrationBuilder.CreateTable(
name: "Bike",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
ModelId = table.Column<int>(nullable: false),
Contact_Name = table.Column<string>(maxLength: 255, nullable: false),
Contact_Email = table.Column<string>(maxLength: 255, nullable: true),
Contact_Phone = table.Column<string>(maxLength: 255, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Bike", x => x.Id);
table.ForeignKey(
name: "FK_Bike_Models_ModelId",
column: x => x.ModelId,
principalTable: "Models",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
As you can see column names are: Contact_Name, Contact_Email, Contact_Phone
.如您所见,列名是:
Contact_Name, Contact_Email, Contact_Phone
。
How to get rid of "_"
to get ContactName
... ?如何摆脱
"_"
以获取ContactName
...?
Name the columns explicitly:明确命名列:
modelBuilder.Entity<Order>().OwnsOne(
o => o.ShippingAddress,
sa =>
{
sa.Property(p => p.Street).HasColumnName("ShipsToStreet");
sa.Property(p => p.City).HasColumnName("ShipsToCity");
});
https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities
Another more complex example (nested owned entity) look this另一个更复杂的例子(嵌套拥有的实体)看这个
empleador.OwnsOne(
property => property.RepresentanteLegal,
configuration =>
{
configuration.Property(repLegal => repLegal.Nombre).HasColumnName("Nombre").HasMaxLength(500);
configuration.OwnsOne(
property => property.Rut,
rutConfiguracion =>
{
rutConfiguracion.Property(rut => rut.DigitoVerificador).HasColumnName("RepLegalRutDv");
rutConfiguracion.Property(rut => rut.Numero).HasColumnName("RepLegalRutNumero");
});
});
You can override the OnModelCreating
method of your DbContext
to avoid naming every column names explicitly.您可以覆盖
DbContext
的OnModelCreating
方法,以避免显式命名每个列名。
The following example will remove the underscores from the column names:以下示例将删除列名中的下划线:
public class MyDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
foreach(var entity in builder.Model.GetEntityTypes())
{
foreach(var property in entity.GetProperties())
{
property.Relational().ColumnName = property.Relational().ColumnName.Replace("_", String.Empty);
}
}
}
}
spidyx answer won't work in the case of Owned types, because their definition sits in navigations. spidyx 答案在拥有类型的情况下不起作用,因为它们的定义位于导航中。
But anywho, I was trying to find a way to do it in a more "reflection fashion way" after OwnsOne's are already set in ModelBuilder, here is a snippet:但是无论如何,在已经在模型构建器中设置了 OwnsOne 之后,我试图找到一种更“反思方式”的方法,这里有一个片段:
private void SetupNamesConvention(ModelBuilder modelBuilder)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
foreach (var navigation in entityType.GetNavigations().Where(x => x.ClrType == typeof(YOUR_OWNS_ONE_PROPERTY)))
{
foreach (var fkProperty in navigation.ForeignKey.DeclaringEntityType.GetProperties())
{
// your code to set naming conventions
...
fkProperty.Relational().ColumnName;
...
}
}
}
}
It may be useful if doing it through:如果通过以下方式进行操作可能会很有用:
.OwnsOne(e => e.Address, cb => { cb.Property(e => e.Postcode).HasColumnName("Postcode"); });
.OwnsOne(e => e.Address, cb => { cb.Property(e => e.Postcode).HasColumnName("Postcode"); });
Is not optimal,like you have some custom logic that goes through every property and sets naming conventions.不是最佳的,就像您有一些自定义逻辑可以遍历每个属性并设置命名约定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.