B.Pumpkin
B.Pumpkin

Reputation: 201

Using .Net Core Identity with generic repository

So I'm trying to get my head around this for a while now, but I don't seem to succeed. In my application I'm using a generic repository with Entity Framework Core. Hence my Repository always expect that it's accessed from a class who's BaseEntity or has inherited from that certain class. Now I want to implement .Net Core Identity with it. But My User class is inheriting from BaseEntity. But I'd also need it to inherit from Identity in order to make it work I guess. How am I able to still use Identity?

Upvotes: 3

Views: 583

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239320

C# only supports single inheritance. You cannot inherit from two different classes. Additionally your Identity user class, must inherit from IdentityUser. You have no choice in that. As a result, the best you can do is make your user class and the rest of your entity classes implement the same interface, i.e. IEntity. Then, instead of constraining your generic type as BaseEntity, use IEntity instead.

Of course, this means you will incur a bit of code duplication as you'll have to implement IEntity separately on both BaseEntity and your user class. However, that is unavoidable.

Upvotes: 2

Related Questions