EF Core 1.1 to WebApi Core - Add-Migration Crash

I think that I have all my dependencies running 1.1 correctly, but when I try to follow the steps here https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db I get a message the error command Add-Migration.

PM> Add-Migration InitialState

An error occurred while calling the "ConfigureServices on startup class" Startup method.

Consider using IDbContextFactory to override DbContext initialization at design time. Error: this method could not find the user secret identifier because the application assembly was not installed. Try using the .AddUserSecrets (Assembly assembly) method instead. no parameterless constructor was found in ApplicationDbContext. Either add the constructor without parameters to ApplicationDbContext or add the implementation of 'IDbContextFactory in the same assembly as' ApplicationDbContext.

Relevant sections of my .json project: ...

"Microsoft.EntityFrameworkCore": "1.1.0", "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0", "Microsoft.EntityFrameworkCore.Design": { "type ": "build", "version": "1.1.0" }, "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final" }, "tools": { "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4-final" }, 

My ApplicationDbContext has a constructor:

 public ApplicationDbContext(DbContextOptions options) : base(options) { } 

, and my Startup.cs has:

 services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 

What else could be?

+6
source share
3 answers

Alternative solution:

I ran into the same problem and landed here. But I am writing an application from scratch, comparing it at every step with DotNetCore WebApp with individual auth. which is generated using the wizard in VS 2017, hoping to follow the latest practices. Therefore, it was strange when everything was identical in my code and the generated code, and I got this exception. The generated code did not offer the code in startup.cs to Michael Dennis, which does not mean that he is wrong, it just means that now there is another way. After further diving, I found out that UserSecretsId was declared in myprojetname.csproj as follows:

 <PropertyGroup> <UserSecretsId>aspnet-mywebproect-006a401f-2fdc-4aad-abb1-12c00000004a</UserSecretsId> </PropertyGroup> 

After adding an entry to the project.csproj file, the problem was resolved.

+1
source

The problem is with calling builder.AddUserSecrets() . To fix, follow these steps:

  • Adding user privacy to the assembly (instead of project.json ) by adding the [assembly: UserSecretsId("aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed")] attribute [assembly: UserSecretsId("aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed")] to Startup.cs

  • In Startup.cs, replace builder.AddUserSecrets() with builder.AddUserSecrets<Startup>();

Reference: InvalidOperationException: Could not find "UserSecretsIdAttribute" in assembly

+5
source

Your problem is not related to EF Core, it's about User Secrets

Check the Startup.cs constructor - it contains a call to AddUserSecrets() . You can delete it. Later, when you read about the user's secrets, you can add it back with the correct configuration (I think you have a website template from 1.0.0, and a link to the library 1.1.0 - it contains this bug fixed )

+1
source

Source: https://habr.com/ru/post/1012711/


All Articles