Npgsql与Entity Framework代码优先集成 [英] Npgsql integration with Entity Framework Code First

查看:107
本文介绍了Npgsql与Entity Framework代码优先集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a project using the last version of EF CF with PostgreSQL and Npgsql.

我的模型如下:

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

并且数据库/表/列的名称都使用小写:

And the database/tables/columns has lowercase names like:

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

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

The Npgsql generates SQL commands with quotation marks so I must use the Data Annotations due the PostgreSQL characteristics, witch is annoying. However I would like to not use quotations delimited names in the database.

有没有一种方法可以配置Npgsql在生成命令或强制小写表/列时不包括引号生成的SQL中的名称?

Is there a way to configure Npgsql to not include quotation marks when generate commands or force lowercase table/columns names in the generated SQL?

推荐答案

如果我不缺少某些内容,则需要一些通用方法更改表的命名约定

If I'm not missing something - you'd want some generic way of changing the naming convention for tables?

EF6具有自定义约定功能-仍不是正式版本,但是如果对您有用,则有些链接...

The EF6 has the custom conventions feature - it's still not official version, but if it works for you, some links...

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

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

In your case, you'd have to implement it for the class/Type I guess - e.g. (some pseudo code)...

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

1) implement IConfigurationConvention<Type, EntityTypeConfiguration> (you can check the EF source for EntityConventionBase)

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

2) In the Apply - change how the Table names are generated via configuration (ToTable()) - to something like .ToLowerCase()

3)添加到约定...

3) add to conventions...

例如...

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 /

You can see one example here
http://blog.cincura.net/233167-custom-conventions-in-entity-framework-6-helping-firebird/

否则,我不知道 Npgsql / PostgreSQL-在我看来确实有些原始。但是您可以在EF方面进行处理。

Otherwise, I have no idea about Npgsql / PostgreSQL - it did seem a bit 'raw' to me. But you can handle it on the EF side.

这篇关于Npgsql与Entity Framework代码优先集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆