1

I have just begun a new project using the ASP .Net Identity 2. It generates a database out of the box for the Identity tables which is fine. My confusion is where do I put my application specific tables? Do I create an entirely separate DbContext & database for them or is it best practice to simply bundle all of the tables into one database together?

Thanks

2 Answers 2

2

If you are using asp.net MVC5 identity 2 then ApplicationDbContext already there in IdentityModels.cs .So you can use that.

   public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
   {
    public ApplicationDbContext()
        : base("ApplicationDbContext", throwIfV1Schema: false)
    {
    }

    public DbSet<Department> Departments { get; set; }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
   }
Sign up to request clarification or add additional context in comments.

4 Comments

But is it best practice? I'm leaning towards the idea of keeping the two separate. It seems a cleaner solution.
I am not sure this is best practice or not.please check this link stackoverflow.com/a/19904081/1388900
@James you can certainly keep 2 contexts, but this adds an overhead to your code. And migrations become harder and you just have more code to maintain. If you have a benefit of having identity context separate, then do it, otherwise have only one context.
Moving the ApplicationUser into my Domain layer seems to be the main barrier to using one context as it inherits from IdentityUser. This would then require my domain layer have a ref to Microsoft.AspNet.Identity which to me seems incorrect. If I use two separate databases, then this problem does not arise.
1

Normally you would extend the IdentityDbContext class and put your application specific tables into this context. This would look something similar to the following

public class BlogContext : IdentityDbContext<ApplicationUser>
{
  public BlogContext()
    : base("BlogConnection")
  {
  }

  public DbSet<Post> Posts { get; set; }
  public DbSet<Comment> Comments { get; set; }
}

There are edge-cases where it is better to separate your application data from the one's hold by Identity. One case could be when your application data is not related to your user data and you want to keep them separate. Nevertheless, when creating a separate context for your application data you should keep in mind that you have to deal with two contexts, which can be painful sometimes.

Comments

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.