mark vanzuela
mark vanzuela

Reputation: 1391

ASP.NET MVC 4 Code first, How add tables using Code first

Im playing currently on visual studio 2012 asp.netmvc4 with simple membership. This will generate 5 tables after running.

My problem is how do i add additional tables? I tried by creating a new model and adding a DbSet on the context class but it will throw error saying dbo.model does not exist in the database. The table is not created.

Is there a standard way of adding a model that will generate a table without running db migrations command?

I even added this Database.SetInitializer(new HWMDContextInitializer()); on application start but the error is still there.

Upvotes: 2

Views: 7070

Answers (3)

aimme
aimme

Reputation: 6808

If you want Entity Framework to drop and regenerate your database automatically whenever you change your model schema, use data migrations.

For more information refer to the documentation: http://msdn.microsoft.com/en-us/data/jj591621.aspx

Upvotes: 0

nzic
nzic

Reputation: 184

Have you tried this?

protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        Database.SetInitializer<YourContext>(new DropCreateDatabaseIfModelChanges<YourContext>());

Upvotes: 0

Related Questions