Exemplo n.º 1
0
 public void EnsureEmptyDb(string dbConn, Action <DbContextOptionsBuilder, string> setupAction)
 {
     using (var dbContext = new AutoCreateEmptyDbContext(dbConn, setupAction))
     {
         if (!dbContext.Database.CanConnect())
         {
             dbContext.Database.EnsureCreated();
         }
     }
 }
Exemplo n.º 2
0
        //public DbContextOptionsBuilder SetupDbContextOptionsBuilder(DbContextOptionsBuilder optionsBuilder, string provider, string dbConn)
        //{
        //    var fixProvider = AutoFixProvider(provider);

        //    if (fixProvider.Equals("SqlServer", StringComparison.OrdinalIgnoreCase))
        //    {
        //        optionsBuilder.UseSqlServer(dbConn);
        //        return optionsBuilder;
        //    }

        //    if (fixProvider.Equals("MySql", StringComparison.OrdinalIgnoreCase))
        //    {
        //        //8.0.19.0 => 8.0.22.0
        //        //System.TypeLoadException:“Method 'get_Info' in type 'MySql.Data.EntityFrameworkCore.Infraestructure.MySQLOptionsExtension'
        //        //from assembly 'MySql.Data.EntityFrameworkCore, Version=8.0.19.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' does not have an implementation.”
        //        optionsBuilder.UseMySQL(dbConn);
        //        return optionsBuilder;
        //    }
        //    throw new NotSupportedException("不支持的数据库类型: " + provider);
        //}

        #endregion

        public void EnsureEmptyDb(string dbConn, string dataProvider)
        {
            var provider = AutoFixProvider(dataProvider);

            using (var dbContext = new AutoCreateEmptyDbContext(dbConn, provider))
            {
                if (!dbContext.Database.CanConnect())
                {
                    dbContext.Database.EnsureCreated();
                }
            }
        }