實體框架7使用代碼第一種方法使表名複數化

asp.net c# entity-framework entity-framework-core

我是ASP / EF的新手。我在我的個人項目中使用ASP 5和Entity Framework 7。

所以我能夠使用代碼優先方法創建數據庫和表,但是所有表名都是單數的,默認情況下不會復數。

在我的ServerMatrixDemoDB DbContext文件中,我在DBSets下面創建了:

public class ServerMatrixDemoDB : DbContext
{
    public ServerMatrixDemoDB()
    {
        Database.EnsureCreated();
    }
    public DbSet<Domain> Domains { get; set; }
    public DbSet<Environment> Environments { get; set; }
    public DbSet<Network> Networks { get; set; }
    public DbSet<OsVersion> OsVersions { get; set; }
    public DbSet<Tier> Tiers { get; set; }
    public DbSet<Service> Services { get; set; }
    public DbSet<HardwareType> HardwareTypes { get; set; }
    public DbSet<Powershell> Powershell { get; set; }
    public DbSet<DotNetVersion> DotNetVersions { get; set; }
    public DbSet<Status> Status { get; set; }
    public DbSet<Servers> Servers { get; set; }
    public DbSet<Application> Applications { get; set; }

}

在我的startup.cs文件中,我使用下面的代碼:

public class ServerMatrixDemoDB : DbContext
{
    public ServerMatrixDemoDB()
    {
        Database.EnsureCreated();
    }
    public DbSet<Domain> Domains { get; set; }
    public DbSet<Environment> Environments { get; set; }
    public DbSet<Network> Networks { get; set; }
    public DbSet<OsVersion> OsVersions { get; set; }
    public DbSet<Tier> Tiers { get; set; }
    public DbSet<Service> Services { get; set; }
    public DbSet<HardwareType> HardwareTypes { get; set; }
    public DbSet<Powershell> Powershell { get; set; }
    public DbSet<DotNetVersion> DotNetVersions { get; set; }
    public DbSet<Status> Status { get; set; }
    public DbSet<Servers> Servers { get; set; }
    public DbSet<Application> Applications { get; set; }

}

一旦我構建並運行項目,就會創建一個數據庫和表,但所有表名都是單數。有沒有辦法在代碼第一種方法中復數表名?

一般承認的答案

您可以在OnModelCreating重載中執行此操作,如 -

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      foreach (var entity in modelBuilder.Model.GetEntityTypes())
      {
        modelBuilder.Entity(entity.Name).ToTable(entity.Name + "s");
      }
    }

您也可以使用“數據註釋”來完成此操作

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      foreach (var entity in modelBuilder.Model.GetEntityTypes())
      {
        modelBuilder.Entity(entity.Name).ToTable(entity.Name + "s");
      }
    }

或流利的Api

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      foreach (var entity in modelBuilder.Model.GetEntityTypes())
      {
        modelBuilder.Entity(entity.Name).ToTable(entity.Name + "s");
      }
    }

有關詳細信息,請參閱 - EF7的文檔


熱門答案

當前代碼將tablespropertieskeysindexes轉換為Postgre snake case,您可以將它用作自定義約定的基礎:

using System;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Npgsql;

namespace Database.Customization
{
    public class PostgreDbContext : DbContext
    {
        private static readonly Regex _keysRegex = new Regex("^(PK|FK|IX)_", RegexOptions.Compiled);

        public PostgreDbContext(DbContextOptions options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            FixSnakeCaseNames(modelBuilder);
        }

        private void FixSnakeCaseNames(ModelBuilder modelBuilder)
        {
            var mapper = new NpgsqlSnakeCaseNameTranslator();
            foreach (var table in modelBuilder.Model.GetEntityTypes())
            {
                ConvertToSnake(mapper, table);
                foreach (var property in table.GetProperties())
                {
                    ConvertToSnake(mapper, property);
                }

                foreach (var primaryKey in table.GetKeys())
                {
                    ConvertToSnake(mapper, primaryKey);
                }

                foreach (var foreignKey in table.GetForeignKeys())
                {
                    ConvertToSnake(mapper, foreignKey);
                }

                foreach (var indexKey in table.GetIndexes())
                {
                    ConvertToSnake(mapper, indexKey);
                }
            }
        }

        private void ConvertToSnake(INpgsqlNameTranslator mapper, object entity)
        {
            switch (entity)
            {
                case IMutableEntityType table:
                    var relationalTable = table.Relational();
                    relationalTable.TableName = ConvertGeneralToSnake(mapper, relationalTable.TableName);
                    if (relationalTable.TableName.StartsWith("asp_net_"))
                    {
                        relationalTable.TableName = relationalTable.TableName.Replace("asp_net_", string.Empty);
                        relationalTable.Schema = "identity";
                    }

                    break;
                case IMutableProperty property:
                    property.Relational().ColumnName = ConvertGeneralToSnake(mapper, property.Relational().ColumnName);
                    break;
                case IMutableKey primaryKey:
                    primaryKey.Relational().Name = ConvertKeyToSnake(mapper, primaryKey.Relational().Name);
                    break;
                case IMutableForeignKey foreignKey:
                    foreignKey.Relational().Name = ConvertKeyToSnake(mapper, foreignKey.Relational().Name);
                    break;
                case IMutableIndex indexKey:
                    indexKey.Relational().Name = ConvertKeyToSnake(mapper, indexKey.Relational().Name);
                    break;
                default:
                    throw new NotImplementedException("Unexpected type was provided to snake case converter");
            }
        }

        private string ConvertKeyToSnake(INpgsqlNameTranslator mapper, string keyName) =>
            ConvertGeneralToSnake(mapper, _keysRegex.Replace(keyName, match => match.Value.ToLower()));

        private string ConvertGeneralToSnake(INpgsqlNameTranslator mapper, string entityName) =>
            mapper.TranslateMemberName(ModifyNameBeforeConvertion(mapper, entityName));

        protected virtual string ModifyNameBeforeConvertion(INpgsqlNameTranslator mapper, string entityName) => entityName;
    }
}



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因