简体   繁体   English

多个数据库的代码优先迁移?

[英]Code-First Migrations for multiple databases?

I have the following connection string: 我有以下连接字符串:

<?xml version="1.0" encoding="utf-8"?>
  <connectionStrings>
    <add name="MyContext" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string='data source=SQLSERVERDB;initial catalog=TestDB_CodeFirst;user id=***;password=***;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

When I try to enable migrations I first get a warning: 当我尝试启用迁移时,我首先收到警告:

Cannot determine a valid start-up project. 无法确定有效的启动项目。 Using project 'MyApp.Model' instead. 改用项目“ MyApp.Model”。
Your configuration file and working directory may not be set as expected. 您的配置文件和工作目录可能未按预期设置。
Use the -StartUpProjectName parameter to set one explicitly. 使用-StartUpProjectName参数可以显式设置一个。

Then I get this exception: 然后我得到这个异常:

Argument 'xmlReader' is not valid. 参数“ xmlReader”无效。 A minimum of one .ssdl artifact must be supplied. 必须至少提供一个.ssdl工件。

Is the connection string wrong and why should I need ssdl if I'm using Code First? 连接字符串是否错误?如果我使用Code First,为什么还需要ssdl?

NOTE 注意

  • My context is in MyApp.Model project where my Migrations folder should be located. 我的上下文在MyApp.Model项目中,该文件夹应位于我的“ Migrations文件夹中。
  • I don't have connection strings in my main startup project because connection strings are retrieved from a second database and the user can select one of them when logging in to the application. 我的主启动项目中没有连接字符串,因为从第二个数据库检索了连接字符串,并且用户在登录应用程序时可以选择其中一个。
  • I have just one connection string shown above in my MyApp.Model project which points to my development database. 我的MyApp.Model项目中只有上面显示的一个连接字符串,它指向我的开发数据库。

Also, my second question is: 另外,我的第二个问题是:

If I use CF migrations, will all databases be migrated each time a user selects a different database for the first time? 如果我使用CF迁移,那么每次用户首次选择其他数据库时,都会迁移所有数据库吗?

EDIT 编辑

I changed the connection as mentioned below, and I get the following exception: 我按如下所述更改了连接,但出现以下异常:

The item with identity 'table1' already exists in the metadata collection. 标识为“ table1”的项目已存在于元数据集合中。 Parameter name: item 参数名称:项目

It must be noted that I reverse-engineered an existing database. 必须指出,我对现有数据库进行了反向工程。 So I don't know what possibly went wrong! 所以我不知道可能出了什么问题!

I've also deleted the Migrations folder and checked the database but there is no migration_history table created. 我还删除了Migrations文件夹并检查了数据库,但是没有创建migration_history表。

You are trying to use a connectionString designed to work with Database First / Model First. 您试图使用一个设计用于数据库优先/模型优先的connectionString。 You can tell because your providerName is System.Data.EntityClient instead of System.Data.SqlClient. 您可以知道,因为您的providerName是System.Data.EntityClient而不是System.Data.SqlClient。

Your connection string should look like this: 您的连接字符串应如下所示:

<connectionStrings>
    <add name="MyContext" 
         connectionString="Data Source=SQLSERVERDB; Initial Catalog=TestDB_CodeFirst;user id=***;password=***;"
        providerName="System.Data.SqlClient" />
</connectionStrings

Although I would suggest using Integrated Security instead of a user/password. 尽管我建议使用集成安全性代替用户名/密码。 Just personal preference, though. 不过只是个人喜好。

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

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