简体   繁体   English

实体框架首先在现有数据库中的单个新架构上使用代码

[英]entity framework use code first on single new schema in existing database

I have a new project I am starting and I would like to use entity framework 6 code first to handle the database portion. 我正在启动一个新项目,我想首先使用实体​​框架6代码来处理数据库部分。 The problem is, I do not have permissions to create a new database on the server I need to use. 问题是,我没有权限在需要使用的服务器上创建新数据库。 Because of this, I would like to create a new schema in an existing database and then have entity framework only interact with that specific schema. 因此,我想在现有数据库中创建一个新的架构,然后让实体框架仅与该特定架构进行交互。 There are many other tables in other schema in the database that are related to other projects (none of which make use of entity framework in any way) and entity framework needs to leave them alone when it is creating/dropping/modifying tables related to my project. 数据库中其他模式中还有许多其他表与其他项目相关(其中任何一个都不以任何方式使用实体框架),并且在创建/删除/修改与我的表相关的表时,实体框架需要将它们保留下来项目。

Is that possible? 那可能吗? If so, how do I go about setting that up? 如果是这样,我该如何进行设置?

It turns out this is actually pretty easy using EF6! 事实证明,使用EF6其实很容易! In your context just override OnModelCreating and add 在您的上下文中,只需重写OnModelCreating并添加

modelBuilder.HasDefaultSchema("schema_name");

before the call to 在致电之前

base.OnModelCreating(modelBuilder);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM