2

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.

2
  • by the way this is using ef5 visual studio 2012 ultimate Commented Sep 8, 2012 at 11:56
  • Scrutinizing the code, i figured it out. There is an attribute InitializeSimpleMembershipAttribute, that sets Database.SetInitializer<HWMDContext>(null); so even if there are changes on the model, the database wont be updated. After I added new DropCreateDatabaseIfModelChanges<YourContext>() replacing the null, it already works. Commented Sep 8, 2012 at 14:17

3 Answers 3

0

Have you tried this?

protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        Database.SetInitializer<YourContext>(new DropCreateDatabaseIfModelChanges<YourContext>());
Sign up to request clarification or add additional context in comments.

2 Comments

added this on application start, Database.SetInitializer<UsersContext>(new DropCreateDatabaseIfModelChanges<UsersContext>()); and i got error: Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.
DropCreateDatabaseIfModelChanges needs the EdmMetadata table to check model compatibility. To set up things you might use DropCreateDatabaseAlways or CreateDatabaseIfNotExists initializers instead.
0

Go through this. It will give you the exact approach http://programmaticponderings.wordpress.com/2012/10/30/first-impressions-of-code-first-development-with-entity-framework-5-in-visual-studio-2012/

Comments

0

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

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.