Abhilash D K
Abhilash D K

Reputation: 1309

Add-Migration Error in Entity Framework Core

I am following Julie Lerman's Getting Started with Entity Framework Core 2.0 tutorial. I have installed Microsoft.EntityFrameworkCore.SqlServer Version 2.0.2. When I try to run add-migration initial (Package Manager Console) in VS 2017 I am obtaining the below error :

System.IO.FileLoadException: Could not load file or assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=2.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) File name: 'Microsoft.EntityFrameworkCore.SqlServer, Version=2.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' When I run dotnet --version i get 2.1.3. Can anybody help me rectify this error.

Screenshot :

enter image description here

Thanks in Advance.

Upvotes: 5

Views: 6351

Answers (2)

Montimur
Montimur

Reputation: 36

Following the very same Pluralsight video, I ran into this same problem. After running:

PM> install-package Microsoft.EntityFrameworkCore.SqlServer

with the default project set to SomeUI

I was able to get the add-migration initial to run without any problems at all, after doing so.

Upvotes: 1

Abhilash D K
Abhilash D K

Reputation: 1309

I know the following is not an exact answer to my own question. But rather than waiting to somebody answer how to solve it in VS 2017 ( Windows ) I am taking a Terminal Approach on my Mac and using dotnet cli to recreate the solution (Julie Lerman hope you won't send your favorite Samurai for a battle :P)

Note: Steps below is for Mac. Windows users having SQl Server Installed can Skip Step 1.

Step 1 : Install Docker and Pull MSSQL Image

a. Install docker and pull latest mssql image for linux using :

sudo docker pull microsoft/mssql-server-linux

b. Start the docker image using :

sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD= 
    <ComplexPassword>' -p 1433:1433 -d microsoft/mssql-server-linux

c. Optional. Install sql-cli using npm install -g sql-cli. Now you can connect to Sql Server running on Docker using mssql -s 127.0.0.1 -u sa -p.

Step 2 : Create .sln, .csproj and add references using dotnet cli

  1. Create directory for Project and under it create a .sln file using: dotnet new sln -n SamuraiApp
  2. Create Data and Domain class Library Projects using : dotnet new classlib -n SamuraiApp.Data and dotnet new classlib -n SamuraiApp.Domain

  3. Create and Empty ASP.NET Core Project using dotnet new web -n SamuraiApp.UI

  4. Add Entity Frmaework Core to Data ClassLibrary using : cd SamuraiApp.Data and dotnet add package Microsoft.EntityFrameworkCore.SqlServer

  5. Add Entity Framework Core Design to UI Project Using : cd SamuraiApp.UI/ and dotnet add package Microsoft.EntityFrameworkCore.Design

  6. Run dotnet restore

Step 3: Add References

  1. Execute dotnet add SamuraiApp.Data reference SamuraiApp.Domain/SamuraiApp.Domain.csproj to add Domain as a reference to Data.
  2. Execute dotnet add SamuraiApp.UI reference SamuraiApp.Domain/SamuraiApp.Domain.csproj to add Domain as reference to UI.
  3. Execute dotnet add SamuraiApp.UI reference SamuraiApp.Data/SamuraiApp.Data.csproj to add Data as reference to UI.
  4. Execute below commands to add all three projects to solution:

    dotnet sln add SamuraiApp.Data/SamuraiApp.Data.csproj dotnet sln add SamuraiApp.Domain/SamuraiApp.Domain.csproj dotnet sln add SamuraiApp.UI/SamuraiApp.UI.csproj

  5. Open SamuraiApp.Data.csproj in your favorite text editor and add the following:

  <ItemGroup>
    <DotNetCliToolReference 
      Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
   </ItemGroup>

To enable dotnet ef.

Step 4 : Write Domain Classes, DataContext class and add migration cd into SamuraiApp.Data folder and execute the below command to add initial migration:

dotnet ef migrations add Initial --startup-project ../SamuraiApp.UI

Hope it helps to somebody. But still In windows using VS 2017 I am facing issues and would hope somebody will help me solve it.

Thanks :)

Upvotes: 0

Related Questions