更改列名约定 [英] Changing Column Name Convention

查看:91
本文介绍了更改列名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CodeFirst中使用EF4。

   public     class     MyContext        DbContext   
{    
   
受保护 覆盖 void OnModelCreating 模型构建器 modelBuilder
  &NBSP; <跨度> { <跨度>
&NBSP; &NBSP; &NBSP; <跨度>碱 <跨度> <跨度> OnModelCreating <跨度>( <跨度>模型构建器 <跨度>); < /跨度> <跨度>

&NBSP; &NBSP; &NBSP; 。模型构建器 <跨度> <跨度>会展 <跨度> <跨度>删除 <跨度>< <跨度> XXX <跨度>>(); <跨度>
&NBSP; &NBSP; &NBSP; 。模型构建器 <跨度> <跨度>会展 <跨度> <跨度>添加 <跨度>< <跨度> XXX <跨度>>()); <跨度>
&NBSP; &NBSP; <跨度>} <跨度>
<跨度>} <跨度>

我应该添加或删除哪些约定来使所有数据库标识符(如列名,表名,存储过程等)都是大写(或不带引号)?


Oracle DB中的案例发送(引用)标识符极大地损害了数据库用户友好性。

解决方案


以下是一些示例代码。无需删除现有约定,因为您的配置将覆盖它们。


 public class MyContext:DbContext 
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Conventions.Add< UpperCaseTables>();
modelBuilder.Conventions.Add< UpperCaseColumns>();
}
}

公共类UpperCaseColumns:
IConfigurationConvention< PropertyInfo,PrimitivePropertyConfiguration>
{
public void Apply(PropertyInfo propertyInfo,Func< PrimitivePropertyConfiguration> configuration)
{
configuration()。ColumnName = propertyInfo.Name.ToUpper();
}
}

公共类UpperCaseTables:
IConfigurationConvention< Type,EntityTypeConfiguration>
{
public void Apply(Type typeInfo,Func< EntityTypeConfiguration> configuration)
{
if(typeInfo.BaseType == typeof(object))
{
configuration()。ToTable(typeInfo.Name.ToUpper());
}
}
}


 


〜罗文


I'm using EF4 with CodeFirst.

public class MyContext : DbContext
{    
   
protected override void OnModelCreating(ModelBuilder modelBuilder)
   
{
     
base.OnModelCreating(modelBuilder);

      modelBuilder
.Conventions.Remove<XXX>();
      modelBuilder
.Conventions.Add<XXX>());
   
}
}

What convention should I add or remove to make all DB identifiers (like column names, table names, stored procedures etc) to be in uppercase (or unquoted)?

Case-sentitive (quoted) identifiers in Oracle DB greatly damage DB user-friendliness.

解决方案

Hi,

Here is some sample code. There is no need to remove the existing conventions as your configuration will override them.

public class MyContext : DbContext
{
  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    base.OnModelCreating(modelBuilder);

    modelBuilder.Conventions.Add<UpperCaseTables>();
    modelBuilder.Conventions.Add<UpperCaseColumns>();
  }
}

public class UpperCaseColumns :
  IConfigurationConvention<PropertyInfo, PrimitivePropertyConfiguration>
{
  public void Apply(PropertyInfo propertyInfo, Func<PrimitivePropertyConfiguration> configuration)
  {
    configuration().ColumnName = propertyInfo.Name.ToUpper();
  }
}

public class UpperCaseTables :
  IConfigurationConvention<Type, EntityTypeConfiguration>
{
  public void Apply(Type typeInfo, Func<EntityTypeConfiguration> configuration)
  {
    if (typeInfo.BaseType == typeof(object))
    {
      configuration().ToTable(typeInfo.Name.ToUpper());
    }
  }
}

 

~Rowan


这篇关于更改列名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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