12

I am using the standard MVC template that comes with VS 2013. It has a neat membership provider that makes using external logins (Google, Facebook etc) a breeze. There are also tutorials on how to extend the IdentityUser model to add new properties such as date of birth.

I would like to add more tables (of my application) to the already coded database context so as to enjoy the same code first migration features. How do I do it? The current db context is defined as follows:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}
Old Geezer
  • 14,854
  • 31
  • 111
  • 198

1 Answers1

26

you are using asp.net MVC5 identity 2 then ApplicationDbContext already there in IdentityModels.cs .So you can add table (DbSet) like this.

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

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

public static ApplicationDbContext Create()
{
    return new ApplicationDbContext();
}
}
Nazmul Hossain
  • 2,085
  • 5
  • 25
  • 34
  • Thanks! It works. My next question: how do I override the Seed method where I can populate some sample rows every time the database is recreated? – Old Geezer Feb 11 '15 at 09:10
  • 1
    Hi, you can do that by implementing a custom database intitializer and overriding the seed method there. have a look here: http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx – Indregaard Feb 11 '15 at 10:48