简体   繁体   中英

Changing column name convention in code-first EF4

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 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM