c# - 更改代码优先 EF4 中的列名称约定

标签 c# entity-framework-4 code-first

我将 EF4 与 CodeFirst 结合使用。

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

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

我应该添加或删除什么约定以使所有数据库标识符(如列名、表名、存储过程等)都为大写(或不带引号)?

Oracle 数据库中区分大小写(带引号)的标识符大大降低了用户友好性。

最佳答案

考虑产品和客户类

public class Product {
    public int Id { get; set; }
    public string Name { get; set; }
    public int Stock { get; set; }
}

public class Customer {
    public int Id { get; set; }
    public string Name { get; set; }
}

public class MyDbContext : DbContext {
    public DbSet<Product> Product { get; set; }
    public DbSet<Customer> Customer { get; set; }

    public MyDbContext() { }

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) {
        modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
        modelBuilder.Conventions.Add<TableNameUppercaseConvention>();
        base.OnModelCreating(modelBuilder);
    }
}

public class TableNameUppercaseConvention : IConfigurationConvention<Type, EntityTypeConfiguration> {
    public void Apply(Type typeInfo, Func<EntityTypeConfiguration> configuration) {
        configuration().ToTable(typeInfo.Name.ToUpper());
    }
}

编辑:

我试过这样做,但似乎行不通,我知道为什么

public class ColumnUppercaseConvention : IConfigurationConvention<MemberInfo, PrimitivePropertyConfiguration> {
    public void Apply(MemberInfo memberInfo, Func<PrimitivePropertyConfiguration> configuration) {
        configuration().ColumnName = memberInfo.Name.ToUpper();
    }
}

我最好的方法是

public class Customer {
    [Column(Name = "ID")]
    public int Id { get; set; }

    [Column(Name = "NAME")]
    public string Name { get; set; }
}

对不起,我现在帮不上忙,但我会继续努力。

关于c# - 更改代码优先 EF4 中的列名称约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4914531/

相关文章:

.net - Entity Framework CTP5 代码优先 : Mapping a class with multiple collections of another class

C# WPF IsEnabled 使用多个绑定(bind)?

c# - DataContract 将继承类型序列化为基类型

c# - 如何使用 EF 5 存储枚举列表

c# - MySQL - 架构无效且未为 .....(等)的实例指定映射

.net - 为什么这个 Entity Framework 代码没有保存到数据库?

c# - 如何运行迁移配置类的 Seed() 方法

c# - 一次向 C# 字典添加多行

c# - 如何在 Visual Studio 2010 中为 C# 安装程序编写自定义操作?

c# - 添加对象时 TransactionContext 有什么区别?