Entity Framework 'could not load assembly' error message

Cure for Entity Framework Migrations could not load assembly error message

I usually setup a new solution so that it contains multiple projects. There's a project for web files, data files, services, utilities and so on. When I downloaded and setup Entity Framework through Nuget I installed it into my Project.Web (where all my views/controllers are) and to my Project.Data (where my data context is located with my repositories) projects.

I installed EF Migrations to my Project.Data project as this is where my data context file lives. Everything compiles and run but sometimes when using Entity Framework Migrations I noticed that I was getting an error:

Could not load assembly 'Project.Data'. (If you are using Code First Migrations inside Visual Studio this can happen if the startUp project for your solution does not reference the project that contains your migrations. You can either change the startUp project for your solution or use the -StartUpProjectName parameter.)

I found that there were a couple of work arounds to this issue:

  • Removing the .suo file and restarting the project help out occasionaly.
  • Clearing the project and rebuilding  it sometimes worked.
  • Other times just running migrations 'Update-Database' multiple times got it working.

None of the above was a silver bullet so I decided to dig a bit further to see what the issue was. It turns out that I needed to do 2 things to fix this issue. The first was that I needed to make sure the Project.Data web.config file contained the connection string to my database and the other was that I needed to specify the start project when running migrations.

So in my Project.Data webconfig I included the connection string:



  

When I want to run EF Migrations I now specify the correct startup project name by first selecting Project.Data from the 'Default Project' drop down in the Package Manager Console and then entering this line:

Update-Database -StartUpProjectName Project.Data

Now when I go to run my migration changes it works all the time as expected.


Share this post


blog comments powered by Disqus

Updates

Follow me online at TwitterFacebook or Flickr. If you want to get in touch with me try emailing me.

Latest Tweets