Asp.net core 从EF6到EF core 3.1的模型创建代码重构

Asp.net core 从EF6到EF core 3.1的模型创建代码重构,asp.net-core,Asp.net Core,我以前试图重构OnModelCreating方法,当时它曾是ASP.net的一部分: protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Types().Configure(c => c.ToTable(c.ClrType.Name.ToUpper())); modelBu

我以前试图重构
OnModelCreating
方法,当时它曾是ASP.net的一部分:

protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
     base.OnModelCreating(modelBuilder);
     modelBuilder.Types().Configure(c => c.ToTable(c.ClrType.Name.ToUpper()));
     modelBuilder.Properties().Configure(c => c.HasColumnName(c.ClrPropertyInfo.Name.ToUpper()));

但是现在在迁移到.netCore 3.1之后,我遇到了这个错误

“ModelBuilder”不包含“Types”的定义,并且找不到接受“ModelBuilder”类型的第一个参数的可访问扩展方法“Types”

你能不能建议一种方法,在保持原有逻辑的同时,正确地重构代码


感谢您的帮助

根据您的代码,您似乎希望更改表名并设置列名,如果是这种情况,您可以尝试引用以下代码来覆盖onmodel创建方法:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{ 
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
        
    }
    public DbSet<Blog> Blogs { get; set; }  

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        //Write Fluent API configurations here

        //Property Configurations 
        modelBuilder.Entity<Blog>().ToTable("BLOGS");
        modelBuilder.Entity<Blog>()
            .Property(b => b.BlogId)
            .HasColumnName("BLOG_ID");

    }
}
 public class SchoolContext : DbContext
 {
     public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
     {
     } 
     public DbSet<Customer> Customers { get; set; }
     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {
         base.OnModelCreating(modelBuilder);
     } 
     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
     {
         base.OnConfiguring(optionsBuilder);
         optionsBuilder.EnableSensitiveDataLogging();
         optionsBuilder
             .UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=TestDB;Trusted_Connection=True;MultipleActiveResultSets=true")       //database connection string.
             .UseUpperCaseNamingConvention();
     }
 }
public类ApplicationDbContext:IdentityDbContext

编辑


那么第二个配置规则呢; modelBuilder.Properties().Configure(c=> c、 HasColumnName(c.ClrPropertyInfo.Name.ToUpper());我过不去 每一列都是大写的

有一个开源插件()可以帮助您。通过使用UseUpperCaseNaming约定,它可以将表和列名更改为大写

您可以参考以下步骤来使用它:

  • 从Nuget中添加

  • 在模型的OnConfigurang方法中启用命名约定:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    { 
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
            
        }
        public DbSet<Blog> Blogs { get; set; }  
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //Write Fluent API configurations here
    
            //Property Configurations 
            modelBuilder.Entity<Blog>().ToTable("BLOGS");
            modelBuilder.Entity<Blog>()
                .Property(b => b.BlogId)
                .HasColumnName("BLOG_ID");
    
        }
    }
    
     public class SchoolContext : DbContext
     {
         public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
         {
         } 
         public DbSet<Customer> Customers { get; set; }
         protected override void OnModelCreating(ModelBuilder modelBuilder)
         {
             base.OnModelCreating(modelBuilder);
         } 
         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
         {
             base.OnConfiguring(optionsBuilder);
             optionsBuilder.EnableSensitiveDataLogging();
             optionsBuilder
                 .UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=TestDB;Trusted_Connection=True;MultipleActiveResultSets=true")       //database connection string.
                 .UseUpperCaseNamingConvention();
         }
     }
    
    公共班级学校上下文:DbContext
    {
    公立学校上下文(DbContextOptions):基本(选项)
    {
    } 
    公共数据库集客户{get;set;}
    模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
    {
    基于模型创建(modelBuilder);
    } 
    配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
    {
    基本配置(选项生成器);
    optionsBuilder.EnableSensitiveDataLogging();
    选项生成器
    .UseSqlServer(“服务器=(localdb)\\mssqllocaldb;数据库=TestDB;可信连接=True;MultipleActiveResultSets=True”)//数据库连接字符串。
    .UseUpperCaseNamingConvention();
    }
    }
    
    迁移后,您可以看到模型快照,它将更改表和列名,如下所示:

    然后,在更新数据库后,该表如下所示:


  • [注意]命名约定是一个由社区维护的插件:它不是Entity Framework Core的官方部分,Microsoft不以任何方式支持它。

    感谢此解决方案,但我希望在保持相同行为的同时重构代码,我不想遍历每一个DB集实体,正如错误消息所说,在EF core中,“ModelBuilder”不包含“类型”的定义。而且,您还可以检查以验证它。因此,您不能在efcore中使用代码(保持相同的行为)。在EF core中,我们必须为每个DB实体配置实体类型和属性。那么第二个配置规则呢;modelBuilder.Properties().Configure(c=>c.HasColumnName(c.ClrPropertyInfo.Name.ToUpper());我不可能把那里的每一个专栏都浏览一遍,就为了成功uppercase@frod_junior请检查我在上面的编辑,并尝试使用命名约定插件将表名和列名更改为大写,我已经在我这方面进行了测试,它与SQL server数据库配合良好,您可以检查它。