但我陷入困境,实体框架在其一个Database.Create()函数中执行两个问题 – 数据库创建然后创建表对象 – 这一点.
有没有办法在没有数据库重新创建的情况下获取表对象创建活动?我希望每个项目共享一个数据库,但具有明确定义的模式所有权.
此代码的主要项目是首先使用代码,因此我们的团队可以同时处理模型的各个部分.此外,该项目不使用迁移,因为我们已在所有部署中使用智能默认设置进行生产.
下面是我到目前为止创建的代码. “// Todo:”部分是我被卡住的地方.
问候
伊恩
namespace app1.Models{ using System; using System.Data.Entity; using System.Data.Entity.Infrastructure.Interception; using System.Diagnostics; using System.linq; public class Model1 : DbContext { public Model1() : base("name=Model1") { // Log database activity this.Database.Log = DeBUGWrite; } private voID DeBUGWrite(string s) { DeBUG.Write(s); } // AvoIDing Compiler Error CS1618 protected overrIDe voID OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("b1"); base.OnModelCreating(modelBuilder); } //public virtual DbSet<blog1> blog1 { get; set; } //public virtual DbSet<common> common { get; set; } } public class DbB1SchemaInitializer : IDatabaseInitializer<Model1> { public voID InitializeDatabase(Model1 context) { context.Database.Log = DeBUGWrite; if (context.Database.Exists()) { if (!context.Database.CompatibleWithModel(true)) { context.Database.Delete(); // Todo: remove this and make delete the schema and its objects context.Database.Create(); // Todo: remove this and make delete the schema and its objects // Reinstall,create schema and application role. context.Database.ExecutesqlCommand("CREATE SCHEMA b1"); context.Database.ExecutesqlCommand("CREATE APPliCATION RolE blog1 WITH PASSWORD = 'Pwd0123456',DEFAulT_SCHEMA = b1"); context.Database.ExecutesqlCommand("GRANT SELECT,UPDATE,INSERT,DELETE,EXECUTE on SCHEMA::b1 to blog1"); } } else { // Fresh install,create the database,schema and application role. context.Database.Create(); // Create will make database and make the tables. context.Database.ExecutesqlCommand("CREATE APPliCATION RolE blog1 WITH PASSWORD = 'Pwd0123456',DEFAulT_SCHEMA = b1"); context.Database.ExecutesqlCommand("GRANT SELECT,EXECUTE on SCHEMA::b1 to blog1"); } // Do database connection interception so database application security is used rather than database user security from this point on. //DbInterception.Add(new EfdbConnectionApplicationRoleInterception("blog1","Pwd0123456")); } private voID DeBUGWrite(string s) { DeBUG.Write(s); } // AvoIDing Compiler Error CS1618 }}解决方法 我不清楚你为什么要这样做,但是如果重新构建模式,也许这可以帮助你:
var command = "IF (NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'b1')) " + "BEGIN" + " EXEC ('CREATE SCHEMA B1');" + " EXEC ('CREATE APPliCATION RolE blog1 WITH PASSWORD = ''Pwd0123456'',DEFAulT_SCHEMA = b1');" + " EXEC ('GRANT SELECT,EXECUTE on SCHEMA::b1 to blog1');" + "END";context.Database.ExecutesqlCommand(command);总结
以上是内存溢出为你收集整理的c# – 实体框架代码首先 – 让它在没有drop数据库的情况下执行“CREATE SCHEMA”?全部内容,希望文章能够帮你解决c# – 实体框架代码首先 – 让它在没有drop数据库的情况下执行“CREATE SCHEMA”?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)