EF 5 Migrations cannot connect to our database even though it does just fine at runtime
Solution 1
Did your start project contains web.config or app.config as EF use the start project as source of the connection string
Solution 2
OK, so that didn't work for me at first :(
Then after a cup of coffee and adding StartUPProjectName to it, it did! Try:
Update-Database -StartUpProjectName MYPROJECT.NAME -Script
Try to point it to a start Up project where you web.config/app.config lives
Solution 3
If you get the help for enable migrations in the Package Manager Console
Get-Help enable-migrations -detailed
You can find this documentation for the -StartupProjectName option:
-StartUpProjectName
Specifies the configuration file to use for named connection strings. If omitted, the specified project's configuration file is used.
The doc it's a little confusing, but it means that if you use this option to specify a project name, migrations will look for the connection string in the config file of the specified project. (The config file can be web.config
or app.config
).
If you're creating a web app, most probably the connection string will be in its web.config
, so you have to specify the web app project. If it's other kind of project, the connection string will be in an app.config
file of a class library or whatever, and you'll have to specify that project.
Besides it's recommended that you use a constructor for your DbContext
class that specifies the name of the connection string, i.e.
public class CompanyEntities : DbContext
{
public CompanyEntities()
:base("ConnectionStringName")
{
...
}
....
}
In this way you don't depend on default connection strings, which may be confusing.
Solution 4
You say you can connect via SQL Management Studio, but my guess is you use Windows Authentication for that, and not the uid=XXXXX;pwd=XXXXX;
supplied in your connection string.
Try to get Management Studio to connect using that userid and password.
Also, that account might be locked out (if it is an Active Directory account).
Solution 5
This sounds eerily like a problem a client of mine had. It had to do with something having mucked up the DbProviders section of machine.config. He put the details here: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/f2a19487-6d38-4a79-a809-d3efe4c9d9fe (it's the Adam Scholz answer to his boss' question. :) )
Julie
Chev
I'm a passionate developer and I love to learn. I also love to share my knowledge with others. Both of those are the primary reasons why I'm here on Stack Overflow :)
Updated on June 17, 2022Comments
-
Chev almost 2 years
We have three projects.
- Company.Domain (class library)
- Company.PublicWebsite (MVC3 Web Application)
- Company.InternalWebsite (MVC3 Web Application)
The two website projects have reference to
Company.Domain
.Our EF 5
DbContext
lives inCompany.Domain.Data.EntityFramework
and it looks like this:using System.Data.Entity; using Company.Domain.Data.EntityFramework.Entities; namespace Company.Domain.Data.EntityFramework. { public class CompanyEntities : DbContext { public DbSet<Notification> Notifications { get; set; } public DbSet<Report> Reports { get; set; } public DbSet<ReportSection> ReportSections { get; set; } public DbSet<ReportPage> ReportPages { get; set; } // brevity brevity } }
We have enabled migrations and successfully used the tool in the past so I'm not sure why we are having issues now. Our migrations configuration lives in
Company.Domain.Data.EntityFramework.Migrations
and looks like this:namespace Company.Domain.Data.EntityFramework.Migrations { using System; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq; using Company.Domain.Data.EntityFramework; public class Configuration : DbMigrationsConfiguration<CompanyEntities> { public Configuration() { MigrationsDirectory = @"Data\EntityFramework\Migrations"; AutomaticMigrationsEnabled = false; } protected override void Seed(CompanyEntities context) { // empty at the moment } } }
We then have an
App.config
file in the root of theCompany.Domain
project and it looks like this:<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> </configSections> <connectionStrings> <add name="CompanyEntities" providerName="System.Data.SqlClient" connectionString="Data Source=devsql;Initial Catalog=CompanyEntities;uid=XXXXX;pwd=XXXXX;MultipleActiveResultSets=True;" /> </connectionStrings> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> </entityFramework> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
Our database lives on another server on the network. I'm able to connect to it in SQL Server Management Studio and our applications are able to connect at runtime just fine. However, when I try to run
add-migration
or evenupdate-database
I get the following error:System.Data.ProviderIncompatibleException: An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct. ---> System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
I've even reverted my changes to the model and then ran
update-database
just to see if it would say 'database is on latest migration' but I still got the above error. I've poured over the connection string inApp.config
over and over. I cannot figure out why migrations won't connect but both of our website projects work just fine at runtime. Migrations have worked in the past. Below are the migrations in solution explorer compared with those found in the__MigrationHistory
table in the database.It looks like I should be able to run
update-database
and have it tell me that it is up to date, but I get that error instead.As I understand it, migrations shouldn't be paying any attention to our two website projects when I'm running migrations commands, but I poured over their Web.config files as well just in case. The connection strings are identical to App.config.
Edit:
I've even tried completely uninstalling and removing the EF 5 package from the projects and reinstalling. Same issue >:(