Entity Framework code first - make it do quot;CREATE SCHEMAquot; without a drop database?(首先是实体框架代码 - 让它执行“CREATE SCHEMA;没有删除数据库?)
问题描述
我正在努力提高数据整合性能和通过备份统一,允许单独的项目在一个数据库中使用单独的架构.
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);
这篇关于首先是实体框架代码 - 让它执行“CREATE SCHEMA";没有删除数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:首先是实体框架代码 - 让它执行“CREATE SCHEMA";没有删除数据库?
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30