0

Currently I have a nice model, and I can generate a database based on that, but from what I can tell, the tables are never created (leading to all sorts of fun runtime errors).

My understanding is that there are three options for code first that would force EF to create the tables for me:

  • DropCreateDatabaseAlways
  • CreateDatabaseIfNotExists
  • DropCreateDatabaseIfModelChanges

How can I use these if I am doing things model first?

Additionally, is this an expected error, or when I selected generate database from model the first time is this supposed to happen automatically?

Edit: I tried calling

            context.Database.Initialize(true);
            context.Database.CreateIfNotExists();

and nothing changes.

soandos
  • 4,978
  • 13
  • 62
  • 96

1 Answers1

2

also this is good toturial tutorial

but if you made the model good the first time you access the dbContext the db should be created by the db strategy which you can set: Database.SetInitializer() set initializer

in short after you create your model you need to create class that inherit from DbContext:

 public class CompanyContext : DbContext
{
    public CompanyContext() : base("CompanyDatabase") { }

    public DbSet<Collaborator> Collaborators { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<Manager> Managers { get; set; }
}

and then when you access this context the tables should be generated. you can also seed the database with data you should inherit from the strategy you want to implement look at this link seeding database

ilay zeidman
  • 2,654
  • 5
  • 23
  • 45
  • If the model is only good now (it was not before) how can I update it to the tables? Generate database from model does not seem to do the trick. Also, I cannot find that method. Do I set it on the context? – soandos Jan 06 '14 at 17:01
  • Yes it should do the trick I did it lot of time you create your own class that inherited from DbContext? – ilay zeidman Jan 06 '14 at 17:03
  • I have created none of my own classes. I need to do that? – soandos Jan 06 '14 at 17:03
  • No it is static method in System.Data.Entity – ilay zeidman Jan 06 '14 at 17:04
  • I want to understand you have just the model classes? – ilay zeidman Jan 06 '14 at 17:05
  • public class YourContext: DbContext { public DbSet nameOfEntity{ get; set; } } you have such class? – ilay zeidman Jan 06 '14 at 17:07
  • Ok, so I have set the model strategy, and now it is telling me that I cannot the database (master) as it is a sytem database. How can I get EF to create a new database (not master, model, tempdb or msdb) so that I will not get this error? – soandos Jan 06 '14 at 17:12
  • I know this error you can change the connection string in the app config to create to you sql compact database or other db like sql express – ilay zeidman Jan 06 '14 at 17:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44638/discussion-between-soandos-and-ilay-zeidman) – soandos Jan 06 '14 at 17:21