1

I am trying to refactor the OnModelCreating method before when it used to be a part of 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()));

....

But now after migrating to .netCore 3.1 I am getting this error

'ModelBuilder' does not contain a definition for 'Types' and no accessible extension method 'Types' accepting a first argument of type 'ModelBuilder' could be found

Could you please suggest a way to refactor the code properly while keeping the same old logic

Thanks for your help

frod_junior
  • 115
  • 2
  • 13

1 Answers1

1

According to your code, it seems that you want to change the Table Name and set the Column Name, if that is the case, you could try to refer the following code to override the OnModelCreating method:

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");

    }
}

More detail information, please check the following links:

EF Core Fluent API Configuration

Entity Types # Table Name

Column names

How to Specify Entity Framework Core Table Mapping?

Edit:

And what about the second config rule ; modelBuilder.Properties().Configure(c => c.HasColumnName(c.ClrPropertyInfo.Name.ToUpper())); I cant go through every column there to just make it uppercase

There have an open source plugin (Naming Conventions) which might help you. By using its UseUpperCaseNamingConvention, it could change the table and Column name to Uppercase.

You could refer to the following steps to use it:

  1. Add the EFCore.NamingConventions from the Nuget.

  2. Enable the naming convention in your model's OnConfiguring method:

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

    After migration, you can see the Model Snapshot, it will change the Table and Column name as below:

    enter image description here

    Then, after update the database, the table like this:

    enter image description here

[Note] The Naming Conventions is a community-maintained plugin: it isn't an official part of Entity Framework Core and isn't supported by Microsoft in any way.

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • Thanks for this solution, but I am looking to refactor the code whilst keeping the same behavior, I don't want to go through every DB set entity and do that – frod_junior Aug 04 '20 at 12:43
  • As the error message said, in EF core, 'ModelBuilder' doesn't contain a definition for 'Types'. And, you could also check the [EF core ModelBuilder class](https://github.com/dotnet/efcore/blob/main/src/EFCore/ModelBuilder.cs) to verify it. So, you can't using the code (keeping the same behavior) in EF core. In EF core, we have to configure the Entity Types and Properties for each DB Entity. – Zhi Lv Aug 05 '20 at 08:10
  • And what about the second config rule ; modelBuilder.Properties().Configure(c => c.HasColumnName(c.ClrPropertyInfo.Name.ToUpper())); I cant go through every column there to just make it uppercase – frod_junior Aug 05 '20 at 08:53
  • @frod_junior Please check my Edit in the above, and try to use the Naming Conventions plugin to change the Table and Column name to Uppercase, I have tested it on my side, it works well with the SQL server database, you could check it. – Zhi Lv Aug 05 '20 at 14:48