public static void InitDb <T>(T db) where T : Afx.Data.Entity.EntityContext { if (!ConfigUtils.InitDatabase || IsInit) { return; } lock (lockObj) { if (IsInit) { return; } string connectionString = ConfigUtils.ConnectionString; IDatabaseSchema databaseSchema = null; ITableSchema tableSchema = null; switch (ConfigUtils.DatabaseType) { case DatabaseType.MSSQLServer: databaseSchema = new Afx.Data.MSSQLServer.Entity.Schema.MsSqlDatabaseSchema(connectionString); tableSchema = new Afx.Data.MSSQLServer.Entity.Schema.MsSqlTableSchema(connectionString); break; case DatabaseType.MySQL: databaseSchema = new Afx.Data.MySql.Entity.Schema.MySqlDatabaseSchema(connectionString); tableSchema = new Afx.Data.MySql.Entity.Schema.MySqlTableSchema(connectionString); break; default: throw new Exception("【InitDatabase】InitDb, 数据库类型错误!"); } //更新数据库结构sql 日志 databaseSchema.Log = WriteSQL; tableSchema.Log = WriteSQL; using (var build = new BuildDatabase(databaseSchema, tableSchema)) { build.Build <T>(); } IsInit = true; } }
protected override void Seed(PropertyContext context) { BuildDatabase DB = new BuildDatabase(); context.SaveChanges(); }