EF Core 1.1 to WebApi Core - Add-Migration fails

.net asp.net-core c# entity-framework entity-framework-core

Question

I think I’ve got all my dependencies running 1.1 properly but when I try to follow the steps here https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db I get an error running the Add-Migration command.

PM> Add-Migration InitialState

An error occurred while calling method ‘ConfigureServices’ on startup class ‘Startup’.

Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Error: This method could not find a user secret ID because the application’s entry assembly is not set. Try using the “.AddUserSecrets(Assembly assembly)” method instead. No parameterless constructor was found on ‘ApplicationDbContext’. Either add a parameterless constructor to ‘ApplicationDbContext’ or add an implementation of ‘IDbContextFactory’ in the same assembly as ‘ApplicationDbContext’.

relevant sections of my project.json: …

 “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 does have the constructor:

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

and my Startup.cs does have the:

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

What else can it be?

1
6
11/29/2016 5:14:48 AM

Accepted Answer

An alternate solution:

I ran into the same problem and landed here. But I am writing an app from scratch comparing it at every step with the DotNetCore WebApp with Individual auth. that gets generated via wizard in VS 2017 hoping to follow latest practices. So it was strange when everything was identical in my code and the generated code and I was getting this exception. The generated code did not have Michael Dennis's suggested code in startup.cs, which does not mean he is wrong, it just means there was now a different way. Upon diving down further I found out that UserSecretsId was declared in myprojetname.csproj like follows:

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

After adding the entry in my project.csproj file, the issue was resolved.

1
4/2/2017 8:38:37 PM

Popular Answer

The issue is related to the builder.AddUserSecrets() call. To fix perform the following steps:

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

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

Reference: InvalidOperationException: Could not find 'UserSecretsIdAttribute' on assembly



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow