aspnet / Identity

[Archived] ASP.NET Core Identity is the membership system for building ASP.NET Core web applications, including membership, login, and user data. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
1.96k stars 869 forks source link

Override property and data no more found #1916

Closed ghost closed 6 years ago

ghost commented 6 years ago

I get this kind of error:

InvalidOperationException: The seed entity for entity type 'AppIdentityRoleClaim' cannot be added because there was no value provided for the required property 'ClaimType'.

When I try to override any property of IdentityXXX class, like for example IdentityRoleClaim<TKey>:

        new public virtual string ClaimType { get; set; }

and then try to get the user in AuthorizationHandler class: var user = await _userManager.GetUserAsync(context.User);

Without overriding, everything works as expected.

This project uses Microsoft ASP.NET Core 2.1.2 Microsoft EntityFrameworkCore 2.1.1

ajcvickers commented 6 years ago

@ckams Using new in that way creates a new property that hides the one on the base class. So there are now two independent properties, both with the same name. Code will use one or the other based on which type it is compiled against. Since the Identity code is not compiled against your types, it will never use or set your new property. But EF is still trying to persist it, and since there is no value set there is an exception.

ghost commented 6 years ago

@ajcvickers Not obvious, even very weired… I find this very buggy, like a lot of things with Identity/Efcore…

Anyway, I tried to override these properties in this app for translation purpose…

So how can I localize this properties?

ajcvickers commented 6 years ago

@ckams Overriding (public override string ClaimType { get; set; }) should work fine. Hiding with new is very different. Using new in this way is quite uncommon.