Npgsql与Entity Framework Code First的集成

Max*_*hen 6 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; }
}
Run Code Online (Sandbox Code Playgroud)

而数据库/表/列的小写名称如下:

CREATE TABLE mytable
{
    id serial,
    mycolumn character(50)
}
Run Code Online (Sandbox Code Playgroud)

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

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

NSG*_*aga 8

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

EF6有这个custom conventions功能 - 它仍然不是正式版,但如果它适合你,一些链接......

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

在你的情况下,你必须为类/ Type我猜它实现它- 例如(一些伪代码)......

1)实现IConfigurationConvention<Type, EntityTypeConfiguration>(你可以检查EF源EntityConventionBase)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 我知道@MaxBündchen - 但这是我认为最好的 - 没有PostgreSQL方面的任何解决方案 (2认同)