Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 5 Migrations cannot connect to our database even though it does just fine at runtime

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 in Company.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 the Company.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 even update-database I get the following error:

http://content.screencast.com/users/Chevex/folders/Snagit/media/80fbfd6a-4956-407f-b88f-d5a53a9e5feb/03.21.2013-10.25.png

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

http://content.screencast.com/users/Chevex/folders/Snagit/media/7abeaa46-ff0f-4817-a0d7-1adb086e8f0c/03.21.2013-10.30.png

http://content.screencast.com/users/Chevex/folders/Snagit/media/3c6ac54d-f63d-417f-9253-39b6a8fea85d/03.21.2013-10.32.png

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 >:(

like image 674
Chev Avatar asked Mar 21 '13 16:03

Chev


2 Answers

Did your start project contains web.config or app.config as EF use the start project as source of the connection string

like image 60
Bhugot Avatar answered Oct 15 '22 14:10

Bhugot


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

like image 35
Ahmed Gadir Avatar answered Oct 15 '22 14:10

Ahmed Gadir