Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

An error occurred while calling method 'ConfigureServices' on startup class #970

Closed
mdmoura opened this issue Sep 18, 2016 · 10 comments
Closed

Comments

@mdmoura
Copy link

mdmoura commented Sep 18, 2016

On an ASP.NET Core 1.0.1 project I have the following context:

public class Context : IdentityDbContext<User, Role, Int32, UserClaim, UserRole, UserLogin, RoleClaim, UserToken> { 
  public Context(DbContextOptions options) : base(options) { }
  protected override void OnModelCreating(ModelBuilder builder) {
    base.OnModelCreating(builder);
  }
}

Where the entities are the following:

public class User : IdentityUser<Int32, UserClaim, UserRole, UserLogin> { }
public class Role : IdentityRole<Int32, UserRole, RoleClaim> { }
public class RoleClaim : IdentityRoleClaim<Int32> { }
public class UserClaim : IdentityUserClaim<Int32> { }
public class UserLogin : IdentityUserLogin<Int32> { }
public class UserRole : IdentityUserRole<Int32> { }
public class UserToken : IdentityUserToken<Int32> { }

On Startup I have the following:

  services.AddDbContext<Context>(x => x.UseSqlServer(connectionString, y => y.MigrationsHistoryTable("__Migrations")));

  services
    .AddIdentity<User, Role>()
    .AddEntityFrameworkStores<Context, Int32>()
    .AddDefaultTokenProviders();

When I run "dotnet ef migrations add "FirstMigration" I get the following error:

An error occurred while calling method 'ConfigureServices' on startup class 'WebProject.Startup'. Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Error: GenericArguments[0], 'WebProject.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'.

Am I missing something or is this a bug with the new IdentityDbContext?

@ketrex2
Copy link
Contributor

ketrex2 commented Sep 20, 2016

I believe your issue is related to (and fixed) in this code commit:
1a0cd3c

This updated version has not been released yet. To get around the issue, you may want to copy the code in the corrected version into your own, custom UserStore as a temporary workaround.

@mdmoura
Copy link
Author

mdmoura commented Sep 20, 2016

@ketrex So I need to copy the UserStore file:

https://github.com/aspnet/Identity/blob/1a0cd3c4d15be988abefd889853b0c6d973db9f4/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/UserStore.cs

But how to register this UserStore in my Startup class?

And will there be a release before 1.1 with this fix?
Version 1.1 is scheduled to Q4 2016 / Q1 2017 so it can take a while.

@ketrex2
Copy link
Contributor

ketrex2 commented Sep 20, 2016

@mdmoura
replace

.AddEntityFrameworkStores<Context, Int32>()

with

services.AddUserStore<YourUserStoreClass<YourContextClassName>>()
services.AddRoleStore<RoleStore<YourRoleClassName, YourContextClassName>>()

.AddEntityFrameworkStores() is just a helper method, if you dig into it, all it does is add the two stores to your service container. You can review it here

@HaoK
Copy link
Member

HaoK commented Sep 21, 2016

Dupe of #855

@HaoK HaoK closed this as completed Sep 21, 2016
@HaoK HaoK added the duplicate label Sep 21, 2016
@mdmoura
Copy link
Author

mdmoura commented Oct 3, 2016

@ketrex

Not sure what the problem is but I added the new UserStore to my code and named it CustomUserStore:

https://github.com/aspnet/Identity/blob/1a0cd3c4d15be988abefd889853b0c6d973db9f4/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/UserStore.cs

And on Startup I added the following:

.AddUserStore<IdentityUserStore<User, Role, Context, Int32, UserClaim, UserRole, UserLogin, UserToken, RoleClaim>>()
.AddRoleStore<RoleStore<Role, Context, Int32, UserRole, RoleClaim>>();

This compiles but when I run it I get the following error on Program/Main:

Cannot instantiate implementation type 'CoreProject.CustomUserStore`9
[CoreProject.User,CoreProject.Role,CoreProject.Context,System.Int32,CoreProject.UserClaim,
CoreProject.UserRole,CoreProject.UserLogin,CoreProject.UserToken,CoreProject.RoleClaim]' 
for service type 'Microsoft.AspNetCore.Identity.IUserStore`1[CoreProject.User]'.

Any idea why this happens?

@ketrex2
Copy link
Contributor

ketrex2 commented Oct 3, 2016

@mdmoura, I'm afraid that's not enough for me to go on. Any chance you can throw your project in a github repo so I can take a look?

That error indicates that your container does not contain all the necessary members to instantiate your custom UserStore. But without your code, it's hard to tell what the problem is.

@mdmoura
Copy link
Author

mdmoura commented Oct 3, 2016

@ketrex I just added to a demo project to github:
https://github.com/mdmoura/Core

If you start the project with will get an error on Program/Main.

Am I missing something?

@ketrex2
Copy link
Contributor

ketrex2 commented Oct 4, 2016

@mdmoura I have sent you a PR that should get you up and running.
PR 1

@mdmoura
Copy link
Author

mdmoura commented Oct 31, 2016

@ketrex Sorry for the delay. Thank you, that worked fine.

@mdmoura
Copy link
Author

mdmoura commented Nov 24, 2016

With ASP.NET Core 1.1 I tried to use again:

.AddEntityFrameworkStores<Context, Int32>()
.AddDefaultTokenProviders();  

But I get the errors:

System.ArgumentException: 'GenericArguments[0], 'MvcApp.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'.'

TypeLoadException: GenericArguments[0], 'MvcApp.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]' violates the constraint of type parameter 'TUser'.

Do we still need to use custom UserStore and RoleStore?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants