I am creating an exemplary simple application which uses Code First EF with Migrations. I have a context object which creates a database for itself if one doesn’t exist yet using defaultConnectionFactory. I was wondering how can I overwrite these settings, to hold control over databse name. Even when I commented out defaultConnectionFactory and put connection string I still get the same old database name. Why?
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<!--<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>-->
<connectionStrings>
<add name="MigrationsDemoConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MigrationsDemo;Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
The database name is in the connection string. I assume that your context has a constructor taking “MigrationsDemoConnection” as argument. So it uses database name “MigrationsDemo”. The connection string is the place to change the database name.
You can also construct your context with a full connection string as argument. You can take the connection string from the config file and change its database name, preferably using
SqlConnectionStringBuilder.