配置实体框架v6代码优先标识符以使用所有CAPS [英] Configure Entity Framework v6 Code-First Identifiers to use ALL CAPS

查看:60
本文介绍了配置实体框架v6代码优先标识符以使用所有CAPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Entity Framework v6在对数据库执行事务时不要在标识符周围使用引号.是否可以配置DbContext来执行此操作而无需为每个实体创建配置文件?

I would like Entity Framework v6 to not use quotes around identifiers when executing transactions against the database. Is it possible to configure the DbContext to do this without creating a configuration file for every entity?

例如:

代替

SELECT "ApplicationId"
FROM "dbo"."Applications";

我想要

SELECT ApplicationId
FROM dbo.Applications;

实体框架将能够正确地将属性和实体正确映射到数据库字段和表吗?

Will Entity Framework be able to correctly map properties and entities to the database fields and tables correctly?

我应该指示我的目标是与Oracle DB交互,而不是尝试消除引号.Oracle将要求在包含小写字母的标识符周围使用引号.因此,我可能应该更改请求以表明我需要所有大写标识符.

Rather than try to eliminate the quotation marks, I should have indicated that my goal is to interface with an Oracle DB. Oracle will require using quotation marks around identifiers that contain lowercase letters. So, I should probably change my request to indicate that I need all uppercase identifiers.

我想出了DbContext的OnModelCreating方法的一部分解决方案,但是它无法处理外键标识符:

I came up with part of a solution in the OnModelCreating method of the DbContext, but it won't handle Foreign Key identifiers:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Properties()
           .Configure(c => c.HasColumnName(c.ClrPropertyInfo.Name.ToUpperInvariant()));
        modelBuilder.Types()
           .Configure(c => c.ToTable(c.ClrType.Name.ToUpperInvariant()));            
    }

推荐答案

您可以将外键属性放在您的类中,然后映射它们的数据库列.像这样如何配置DbContext可以与Oracle ODP.Net和EF CodeFirst一起使用?

You can put the foreign key properties in your class, and then map their database column. Like this How to configure DbContext to work with Oracle ODP.Net and EF CodeFirst?

或者,如果您不想将外键属性放在类中,则应使用流畅的API使用大写字母映射外键.像这个例子:

Or, if you don't want to put the foreign key property in the classes, you should use the fluent API to map the foreign key using uppercase letters. Like this example:

modelBuilder.Entity<Course>() 
    .HasRequired(c => c.Department) 
    .WithMany(t => t.Courses) 
    .Map(m => m.MapKey("CHANGEDDEPARTMENTID"));

这篇关于配置实体框架v6代码优先标识符以使用所有CAPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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