2

I am attempting to get away from code-first migrations.

The problem is that AspNetUsers is extending IdentityUser. As a result, when I try to use a DbContext then I get the following errors.

  • EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
  • EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.

I want to use AspNetUsers for two purposes:

  1. Identity management.
  2. Querying users from the database.

Is it possible to go this route without having to use IdentityDbContext?

public class MyEntities : DbContext
{
     public DbSet<AspNetUsers> AspNetUsers { get; set; }
}
1

3 Answers 3

3

you don't need to add

public DbSet<AspNetUsers> AspNetUsers { get; set; }

inherit your context from IdentityDbContext to have only one context

public class MyEntities : IdentityDbContext<AspNetUsers>

also don't forget to set up UserManager to use your MyEntities context with AspNetUsers

Sign up to request clarification or add additional context in comments.

1 Comment

That worked, thank you sir. I had already taken care of the MyEntities context with AspNetUsers. But, it was all relevant so I mark yours as the answer.
2

You could avoid using the IdentityDbContext but you need to setup the mapping yourself in the OnModelCreating method within your DbContext class.

However, if you are using the SignInManager etc.. I recommend you use the IdentityDbContext. You are not constraint to use Code-First Migrations.

1 Comment

I did not need to add anything to the OnModelCreating. Thank you for responding.
1

Identity context inherits from DBContext. Hence as long as your own db context inherits from identity context, it should be enough. Something like following :-

public class MyDbContext : IdentityContext {}

1 Comment

This was also part of the answer, but Nikolay provided additional context that was necessary. I thank you for responding to this post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.