ASP.NET Core 2.0 Identity add new model - table to the Database

1

I have a started a new ASP.NET Core 2.0 project and I use the the Entity Framework with Identity.

The starting database schema consists from the following tables:

enter image description here

Everything works as expected with the current schema and I didn't have any problems with the existing datatable operations.

Now I want to alter the database schema. I want to add some new tables but I am finding it difficult to find some documentation to help me understand the way to do it.

For now I am following the "Create an app with user data protected by authorization" and I started from creating a new model to my application.

After creating a new model and scaffolding the Controller for this model a new Context was created in my appsettings.json. I just had the DefaultConnection and now was added the esendContext.

"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-esend-069757D7-82B1-4850-8015-07D7D54AA712;Trusted_Connection=True;MultipleActiveResultSets=true",
"esendContext": "Server=(localdb)\\mssqllocaldb;Database=esendContext-25459d27-29ec-4943-ac91-6b9cb91a0dd6;Trusted_Connection=True;MultipleActiveResultSets=true"

When I hit the new url I get the following message:

enter image description here

I understand that I should apply the migrations but I wonder why do I get the SqlException: Cannot open database .... error

Furthermore when I try to apply the migration (Add-Migration Initial -Context esendContext according to this since I got an error about the context I should use) according to documentation in the PMC

Install-Package Microsoft.EntityFrameworkCore.Tools
Add-Migration Initial
Update-Database

when I try to update the database I get

An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: Could not load file or assembly 'Microsoft.EntityFrameworkCore, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

No DbContext was found in assembly 'esend'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

I am not really sure If this is the procedure I should be following. Any help or link for something that could help would be greatly appreciated and excuse me If I have stated too many issues in one question.

c#
asp.net-mvc
entity-framework
asp.net-core-2.0
asked on Stack Overflow Dec 29, 2017 by Anastasios Selmanis • edited May 9, 2018 by Anastasios Selmanis

2 Answers

2

Make sure that your .csproj has that node :

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

and i don't know what do you mean by this : "After creating a new model and scaffolding the Controller for this model a new Context was created in my appsettings.json."

but if you are using asp identity then your project has a DbContext already and you don't need a new One !

answered on Stack Overflow Dec 29, 2017 by Mustafa Saeed
0

From what I saw my error was related to the version of the Microsoft.AspNetCore.All I was using which was 2.0.0.

According to this issue and bricelam's answer I updated to version 2.0.3 which was the latest available and the migration was completed correctly.

answered on Stack Overflow Dec 29, 2017 by Anastasios Selmanis

User contributions licensed under CC BY-SA 3.0