Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core 1.1 to WebApi Core - Add-Migration fails

I think I’ve got all my dependencies running 1.1 properly but when I try to follow the steps here https://learn.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?

like image 807
Michael Dennis Avatar asked Nov 29 '16 05:11

Michael Dennis


3 Answers

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

like image 60
Michael Dennis Avatar answered Oct 20 '22 06:10

Michael Dennis


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.

like image 22
Hassan Avatar answered Oct 20 '22 05:10

Hassan


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

Check your Startup.cs constructor - it contains AddUserSecrets() call. You can remove it. Later, when you read about User Secrets, you can add it back with correct configuration (I guess you have website template from 1.0.0, while referencing library 1.1.0 - it contains this bug fix)

like image 1
Dmitry Avatar answered Oct 20 '22 05:10

Dmitry