Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
Your configuration file and working directory may not be set as expected.
Use the -StartUpProjectName parameter to set one explicitly.

Then I get this exception:

Argument 'xmlReader' is not valid. A minimum of one .ssdl artifact must be supplied.

Is the connection string wrong and why should I need ssdl if I'm using Code First?

NOTE

  • My context is in MyApp.Model project where my Migrations folder should be located.
  • 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.

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?

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. 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.

like image 510
Ivan-Mark Debono Avatar asked Apr 17 '26 08:04

Ivan-Mark Debono


1 Answers

You are trying to use a connectionString designed to work with Database First / Model First. You can tell because your providerName is System.Data.EntityClient instead of 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.

like image 99
Dismissile Avatar answered Apr 18 '26 22:04

Dismissile



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!