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

问题描述 投票:0回答:3

我有实体类,类似这样:

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 ...?

c# asp.net-core ef-core-2.1
3个回答
2
投票

显式命名列:

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


1
投票

另一个更复杂的示例(嵌套的实体),看这里

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

0
投票

您可以重写OnModelCreatingDbContext方法,以避免显式命名每个列名。

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

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);
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.