首先是实体框架代码-使其“创建架构"没有删除数据库? [英] Entity Framework code first - make it do "CREATE SCHEMA" without a drop database?
问题描述
我正在尝试改善数据整合性能,统一备份,允许单独的项目在一个数据库中使用单独的架构.
I am trying to do better data consolidation performance & backup unity by, allowing separate project to use separate schema within one database.
但是我很困惑,实体框架在它的一个Database.Create()函数中执行两个问题-创建数据库然后创建表对象.
But I am stuck and the point where entity framework performs two concerns - database creation then table object creation - within its one Database.Create() function.
有没有一种方法可以只获取表对象创建活动而无需重新创建数据库?我希望每个项目共享一个数据库,但具有明确定义的架构所有权.
Is there a way to just get the table object creation activity without database re-creation? I hope to have every project sharing one database but with well defined schema ownership.
此代码的主要项目是首先使用代码,因此我们的团队可以同时处理模型的各个部分.另外,该项目不使用迁移,因为我们已经在生产的所有部署中使用了智能默认设置.
下面是我到目前为止创建的代码."//TODO:"部分就是我遇到的问题.
Below is the code I have created so far. The "//TODO:" part is where I am stuck.
问候伊恩
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, UPDATE, INSERT, DELETE, 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
}
}
推荐答案
对我来说,为什么要这样做尚不十分清楚,但是如果重新创建架构是问题所在,也许可以为您提供帮助:
Its not totally clear to me why you want to do this, but if the recreation of the schema is the issue, maybe this can help you:
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, UPDATE, INSERT, DELETE, EXECUTE on SCHEMA::b1 to blog1');" +
"END";
context.Database.ExecuteSqlCommand(command);
这篇关于首先是实体框架代码-使其“创建架构"没有删除数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!