开发者

Changing column name convention in code-first EF4

开发者 https://www.devze.com 2023-02-09 01:58 出处:网络
I\'m using EF4 with CodeFirst. public class MyContext : DbContext { protected override void OnModelCreating(ModelBuilder modelBuilder)

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<XX开发者_运维百科X>());
   }
}

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 suck in user-friendliness.


Consider the Product and Customer class

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());
    }
}

EDIT:

I tried to do something like this, but it seems that doesn't work, idk why

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

my best approach was

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

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

sorry, I can't help right now, but I'll keep trying.


I don't know how can you do this with conventions but take a look at StoreNameAttribute. You can apply on context, entities and properties.

Data Annotations in Entity Code First

0

精彩评论

暂无评论...
验证码 换一张
取 消