实体框架7使用代码第一种方法复数表名

时间:2016-01-09 23:42:22

标签: c# asp.net 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 void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()              // Add MVC Dependency.
        .AddJsonOptions(
            opt =>
            {
                opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); // Api convert all property names to CamelCase.
            }
        );
    services.AddLogging();          // Enable Logging for database errors.
    services.AddEntityFramework()
      .AddSqlServer()
      .AddDbContext<ServerMatrixDemoDB>(options =>
      {
          options.UseSqlServer(@"Server=.\SQLEXPRESS;user id=sa;password='passwrd';Database=ServerMatrixDemoDB;integrated security=True;");
      });
    services.AddTransient<ServerMatrixDemoSeedData>();
}

一旦我构建并运行项目,就会创建一个数据库和表,但所有表名都是单数。有没有办法在代码第一种方法中复数表名?

4 个答案:

答案 0 :(得分:11)

你可以在OnModelCreating重载中执行此操作,如 -

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

您也可以使用&#34;数据注释&#34;

来完成此操作
    [Table("blogs")]
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    }

或Fluent Api

class MyContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>()
                .ToTable("blogs");
        }
    }

有关详细信息,请查看 - Documentation for EF7

答案 1 :(得分:0)

当前代码将tablespropertieskeysindexes转换为Postgre的蛇案例,您可以将其用作自定义约定的基础:

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

答案 2 :(得分:-1)

也可以使用PluralizationService并创建一个继承自TableAttribute的属性:

/// <summary>
/// Specifies the database table that a class is mapped to with pluralization.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "We want users to be able to extend this class")]
public class PluralizedTableAttribute : TableAttribute
{
    private const string _englishCultureName = "en-us";

    /// <summary>
    /// Initializes a new instance of the <see cref="PluralizedTableAttribute"/> class.
    /// </summary>
    /// <param name="name">The table name.</param>
    public PluralizedTableAttribute(string name)
        :base(PluralizationService.CreateService(CultureInfo.GetCultureInfo(_englishCultureName)).Pluralize(name)) // pluralization only suported in English.
    {
    }
}

你就像使用AtbleAttribute一样使用它,除非现在所有东西都会被luralized

答案 3 :(得分:-1)

 private const string _englishCultureName = "en-us";

    public string GetPuralTableName(string singularTableName)
    {
        // pluralization only suported in English.
        return PluralizationService.CreateService(CultureInfo.GetCultureInfo(_englishCultureName)).Pluralize(singularTableName);

    }