繁体   English   中英

Entity Framework Core 2 拥有的实体类型 - 更改表列的名称

[英]Entity Framework Core 2 Owned Entity Types - Change Names of Table Columns

我有实体类,如下所示:

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

它会默认生成表:

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

如您所见,列名是: Contact_Name, Contact_Email, Contact_Phone
如何摆脱"_"以获取ContactName ...?

明确命名列:

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

另一个更复杂的例子(嵌套拥有的实体)看这个

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

您可以覆盖DbContextOnModelCreating方法,以避免显式命名每个列名。

以下示例将删除列名中的下划线:

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 答案在拥有类型的情况下不起作用,因为它们的定义位于导航中。

但是无论如何,在已经在模型构建器中设置了 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;
                ...
            }
        }
    }
}

如果通过以下方式进行操作可能会很有用:

.OwnsOne(e => e.Address, cb => { cb.Property(e => e.Postcode).HasColumnName("Postcode"); });

不是最佳的,就像您有一些自定义逻辑可以遍历每个属性并设置命名约定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM