[Memo] Points to note when upgrading EntityFramework 6 to EntityFrameworkCore

    I am upgrading a .net framework 4.5 project to .net core 2.1, which uses EF6 and has undergone some modifications:

  1. Changes in the namespace can basically be supplemented by automatic prompts, and there is no need to memorize them.
  2. The return result of DbQuery<T> is changed to: IQueryable<T>.
  3. The Include method already supports incoming types, so extensions are no longer required.
  4. DbContext.Configuration.AutoDetectChangesEnabled property moved to: DbContext.ChangeTracker.AutoDetectChangesEnabled.
  5. In a custom class that inherits from DbContext, the connection string can be defined using the OnConfiguring override method, such as:
        public  partial  class SenparcEntities: DbContext
        {
            ///  <summary> 
            /// Connection String
             ///  </summary> 
            internal  string ConnectionString { get ; set ; }
    
    
            public DbSet<Account> Accounts { get; set; }
            public DbSet<SystemConfig> SystemConfigs { get; set; }
    
            public SenparcEntities(string connectionString)
            {
                ConnectionString = connectionString;
            }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                // Configure the connection string 
                optionsBuilder.UseSqlServer(ConnectionString);
    
                base.OnConfiguring(optionsBuilder);
            }
        }

     

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325115904&siteId=291194637