10

I am using EF 4.1 and have created a repository using DBContext etc. Connection string set to point to a SQL Server 2008 R2 Dev edition.

When I run a console app that calls my gateway which in turn adds an entity and saves, an exception is thrown saying it can't find the table. When I look at my db, there is a database created but there are no tables created automatically except EmdMetadata.

Am I missing something?

Mark

4 Answers 4

8

To set the auto drop and create you would do something like this...

public class MyDbContext : DbContext 
{
    public IDbSet<Foo> Foos { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer(new MyDbContextInitializer());

        base.OnModelCreating(modelBuilder);
    }
}

public class MyDbContextInitializer : DropCreateDatabaseIfModelChanges<MyDbContext>
{
    protected override void Seed(MyDbContext dbContext)
    {
        // seed data

        base.Seed(dbContext);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That looks like what I need, I couldn't call that in the app as I had a layer over the Dbcontext in another dll, keeping dbcontext internal.
This did not fix my problem but for those still getting to this, OnModelCreating takes DbModelBuilder instead of just ModelBuilder. Not sure when it changed.
8

You can create the tables yourself - the easiest way is:

IObjectContextAdapter adapter = (IObjectContextAdapter)context;
string script = adapter.ObjectContext.CreateDatabaseScript();
context.Database.ExecuteSqlCommand(script);

Where context is my EF 4.1 database context. Prior to this code I drop all the tables (from the last time I created the db), and after this I seed it with data.

5 Comments

Really? I was following a few blog-tutorials and they seemed to have theirs auto-generated when they called save.
I've only been using it for a month, and this was the only way I could reliably drop, rebuild and seed my database. Perhaps I gave up too easily.
I've looked at a couple of tutorials (found on google) - one included some sample SQL create scripts, one as you say creates the database automatically. Like you, I found that this did not work for the db structure I created, which included some referential integrity and cascading deletes.
@Mark. Its worth looking at shuniar's answer below, but if it doesn't work, I realized my answer had a mistake in it - corrected code above (i'd used a method I'd created and you would not have).
Thanks for your answer @iandotkelly, I've not been able to find any other way to create "just" the tables without "drop creating" the entire database!
1

Better: add the initializer to the static constructor, like this:

public class MyDbContext : DbContext 
{
    static MyDbContext()
    {
        Database.SetInitializer(new MyDbContextContextInitializer());
    }
}

Comments

-1

You need to add this to your Application_Start()

 Database.SetInitializer(new MyDbContextContextInitializer());
 var context = new MyDbContextContext();
 context.Database.Initialize(true);

The last line forces the DB to created

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.