0

I have created a test project where I am playing around with .NET Core 2.2 and Identity Framework 2.0.

I have successfully created a project and adjusted some fields so that I understand how the data is parsed from the View to the Controller to the Stores.

I have been looking at customizing the database so that a user will also contain a ReferralID to test some stuff out, this ReferralID is an integer.

What I did is I extended the IdentityUser with this custom property and made a custom version for every Identity Option.

    public class ApplicationUsers : IdentityUser
    {
        public int? ReferralID { get; set; } = null;       
    }

I also added it to the database in the tables that were generated but now the application is throwing an error:

CLR property 'ReferralID' cannot be added to entity type 'IdentityUser' because it is declared on the CLR type 'ApplicationUsers'

I have unfortunately not been able to find/understand what is wrong, it seems that something is wrong in the ModelBuilder, I have not touched the model builder and it was auto generated by using a scaffolder Scaffold-DbContext "Data Source=(localdb)\mssqllocaldb;Initial Catalog=CoreTest;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -Context GolfScoresContext -OutputDir Models -DataAnnotations -Project WebApi -Force

What step did I miss? Or can I not add custom properties to an IdentityUser? I have seen people struggling with this and adding [NotMapped] to ApplicationUsers but then I think my property will not get saved.

jps
  • 20,041
  • 15
  • 75
  • 79
Jean-Paul
  • 380
  • 2
  • 9
  • 26

1 Answers1

0

I eventually found out that the reason for throwing it should be something with HasKey and it not accepting an extended model. (using: Entity Framework Core Using multiple DbContexts) I ended up regenerating the model which fixed it, I think some thing broke in the OnModelCreating as suggested here https://github.com/aspnet/EntityFrameworkCore/issues/9133

Jean-Paul
  • 380
  • 2
  • 9
  • 26