I have a dll that contains EF related code: entities, context and code based configuration. When I run tests on this dll, everything works fine.
I have created a ASP MVC Web Api project and when I invoke the dll from it I get following exception:
“Cannot attach the file 'C:\project\xxx\ MyProj.Persistance.IndicatorsContext.mdf' as database MyProj.Persistance.IndicatorsContext' “
I am using code-based configuration:
public class MyDbConfiguration : DbConfiguration
{
public MyDbConfiguration()
{
SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));
}
}
Here is my Context:
[DbConfigurationType(typeof(MyDbConfiguration))]
public class IndicatorsContext : DbContext
{
public IndicatorsContext()
{
Debug.Write("Connection String:" + Database.Connection.ConnectionString);
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<IndicatorsContext>());
}
// etc...
}
}
The funny thing is, when I take a look at connection string in Context constructor, it is different depending if I run the tests or I run Asp Mvc. I have noted that two connection strings differ:
- Data Source=(localdb)\v11.0;Initial Catalog=MyProj.Persistance.IndicatorsContext;Integrated Security=True;MultipleActiveResultSets=True
- Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|MyProj.Persistance.IndicatorsContext.mdf;Initial Catalog=MyProj.Persistance.IndicatorsContext;Integrated Security=True;MultipleActiveResultSets=True
Second one is from Asp Mvc and has AttachDbFilename section?
I have commented-out entityFramework tag in my Web.config and left only the following:
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
How to get MVC project to use the same connection string I get when i run project from the test?