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();
    }
}

1 Answer 1

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();
}
}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
Hi, you can do that by implementing a custom database intitializer and overriding the seed method there. have a look here: entityframeworktutorial.net/code-first/…

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.