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

James
  • 1,979
  • 5
  • 24
  • 52

2 Answers2

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();
    }
   }
Nazmul Hossain
  • 2,085
  • 5
  • 25
  • 34
  • But is it best practice? I'm leaning towards the idea of keeping the two separate. It seems a cleaner solution. – James Dec 17 '14 at 14:06
  • 2
    I am not sure this is best practice or not.please check this link http://stackoverflow.com/a/19904081/1388900 – Nazmul Hossain Dec 17 '14 at 14:12
  • @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. – trailmax Dec 17 '14 at 17:24
  • 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. – James Dec 18 '14 at 10:18
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.

Horizon_Net
  • 5,959
  • 4
  • 31
  • 34