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

发布于 2024-10-16 00:57:52 字数 430 浏览 3 评论 0原文

我将 EF4 与 CodeFirst 结合使用。

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

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后知后觉 2024-10-23 00:57:53

我不知道如何使用约定来做到这一点,但看看 StoreNameAttribute。您可以应用于上下文、实体和属性。

实体代码优先中的数据注释

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

一抹苦笑 2024-10-23 00:57:52

考虑产品和客户类

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

抱歉,我现在无能为力,但我会继续努力的

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文