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

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
Mark
  • 1,538
  • 5
  • 24
  • 31

4 Answers4

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.

iandotkelly
  • 9,024
  • 8
  • 48
  • 67
  • Really? I was following a few blog-tutorials and they seemed to have theirs auto-generated when they called save. – Mark Aug 30 '11 at 22:30
  • 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. – iandotkelly Aug 30 '11 at 22:36
  • 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. – iandotkelly Aug 30 '11 at 22:41
  • @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). – iandotkelly Aug 31 '11 at 03:41
  • 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! – Tejas Sharma Jun 18 '15 at 14:20
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);
    }
}
shuniar
  • 2,592
  • 6
  • 31
  • 38
  • 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. – Mark Aug 31 '11 at 08:32
  • 3
    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. – Pieter Sep 06 '11 at 18:20
1

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

public class MyDbContext : DbContext 
{
    static MyDbContext()
    {
        Database.SetInitializer(new MyDbContextContextInitializer());
    }
}
Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
-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

Simon
  • 415
  • 1
  • 4
  • 15