Npgsql与Entity Framework Code First的集成

时间:2013-04-04 14:50:39

标签: entity-framework npgsql

我有一个项目使用最新版本的EF CF和PostgreSQL以及Npgsql。

我的模型看起来像:

[Table("mytable")]
public class MyTable
{
    [Column("id")]
    public int Id { get; set; }
    [Column("mycolumn")]
    public string MyColumn { get; set; }
}

数据库/ tables / columns的小写名称如下:

CREATE TABLE mytable
{
    id serial,
    mycolumn character(50)
}

Npgsql使用引号生成SQL命令,因此我必须使用由于PostgreSQL特性引起的数据注释,这很烦人。但是,我不想在数据库中使用引号分隔名称。

在生成命令时是否有办法将Npgsql配置为不包含引号或在生成的SQL中强制使用小写表/列名称?

2 个答案:

答案 0 :(得分:8)

如果我没有遗漏某些东西 - 你想要一些通用的方式of changing the naming convention for tables

EF6具有custom conventions功能 - 它仍然不是官方版本,但如果它适用于你,有些链接......

http://entityframework.codeplex.com/wikipage?title=Custom%20Conventions

在你的情况下,你必须为班级/ Type实施它,我想 - 例如(一些伪代码)......

1)实施IConfigurationConvention<Type, EntityTypeConfiguration>(您可以查看EntityConventionBase的EF来源)

2)在Apply中 - 通过配置(ToTable())更改表名的生成方式 - 改为.ToLowerCase()

3)添加约定......

例如......

public class SmallCapsEntitiesConfigurationConvention
    : IConfigurationConvention<Type, EntityTypeConfiguration>
{
    public void Apply(Type memberInfo, Func<EntityTypeConfiguration> configuration)
    {
        configuration().ToTable(memberInfo.Name.ToLowerInvariant(), null);
    }
}

你可以在这里看到一个例子 http://blog.cincura.net/233167-custom-conventions-in-entity-framework-6-helping-firebird/

否则,我不知道Npgsql / PostgreSQL - 它对我来说似乎有些“原始”。但你可以在EF方面处理它。

答案 1 :(得分:0)

以下是EF Core的示例。

当前代码将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;
    }
}