简体   繁体   English

实体框架代码优先-指定要使用的服务器,但不指定数据库

[英]Entity Framework Code First - Specify server to use but not the database

I am trying to use EF CF to create multiple databases using the same schema. 我正在尝试使用EF CF使用相同的架构创建多个数据库。 I want the flexibility to be able to specify which server I want my database created in, and specify the database name during runtime. 我希望能够灵活地指定要在其中创建数据库的服务器,并在运行时指定数据库名称。

So, in sum, given a server, I want to be able to specify what database to create (or use existing) in runtime. 因此,总而言之,给定一台服务器,我希望能够指定要在运行时创建(或使用现有数据库)的数据库。 AFAIK, using the connection string forces me to specify the database as well, so that's not good. AFAIK,使用连接字符串会迫使我也指定数据库,所以不好。

A more thorough search came up with this, and it works just as I needed. 对此进行了更彻底的搜索,它可以按照我的需要工作。

Add this to the configuration file 将此添加到配置文件

 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 
    <parameters> 
      <parameter value="Data Source=MyDatabaseServer; Integrated Security=True; MultipleActiveResultSets=True" /> 
    </parameters> 
  </defaultConnectionFactory> 

where "MyDatabaseServer" could be your ".\\SQLEXPRESS" instance, or whatever. 其中“ MyDatabaseServer”可以是您的“。\\ SQLEXPRESS”实例,或其他任何实例。

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

相关问题 首先使用实体​​框架代码与nosql数据库 - Use Entity framework code first with nosql database 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach? 如何使用实体框架生成与SQL Server兼容的数据库(代码优先) - How to generate a SQL Server compatible database with Entity Framework (code first) Entity Framework Code First 在服务器资源管理器中找不到数据库 - Entity Framework Code First can't find database in server explorer 实体框架代码首先在SQL Server Management Studio中没有数据库 - Entity Framework Code First no database in SQL Server Management Studio 实体框架首先在现有数据库中的单个新架构上使用代码 - entity framework use code first on single new schema in existing database 在实体框架中首先创建数据库和代码 - Creating database first and code first in Entity Framework 在实体框架中使用数据库优先实体和代码优先实体 - Using database first entity with code first entity in entity framework 实体框架数据库优先-SQL Server - Entity Framework database-first with SQL Server MySql数据库中的实体框架代码优先迁移 - Entity Framework Code First Migration in MySql DataBase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM