Database refactoring with EntityFramework from code

I'm in the process of adapting the MVCMusicStore basket. I want to reorganize the database using EntityFramework - not through Server Explorer.

In my model folder, I created a new FooEntities model.

 public class FooStoreEntities : DbContext { public DbSet<Foo> Foos { get; set; } public DbSet<Category> Categories { get; set; } public DbSet<FooMaker> FooMakers { get; set; } public DbSet<Cart> Carts { get; set; } public DbSet<Order> Orders { get; set; } public DbSet<OrderDetail> OrderDetails { get; set; } } 

I also created additional models Foo , FooMaker and Category . Other models already existed in the MVCMusicStore example.

I created the SampleFooData class, which inherits from DropCreateDatabaseIfModelChanges<FooStoreEntities> and overrides the seed method to sow new tables.

I changed the Application_Start method in Global.asax.cs containing System.Data.Entity.Database.SetInitializer(new FooStore.Models.SampleFooData());

In the web.config , I have:

 <connectionStrings> <add name="FooStoreEntities" connectionString="Data Source=|DataDirectory|FooStore.sdf" providerName="System.Data.SqlServerCe.4.0"/> </connectionStrings> 

Where FooStore.sdf still contains all the old tables from the MVCMusicStore application.

When I run the web application, it hits the SetInitializer breakpoint, but it doesn't seem to create a SampleFooData object or create new tables in the database. What am I missing? Or something fundamental I don’t understand?

+4
source share
1 answer

It will be created only when using the database context: see Entity Framework Database.SetInitializer just doesn't work

You can put

 var ctx = new FooStoreEntities(); ctx.Database.Initialize(true); 

after you set the initializer in Application_Start.

+2
source

All Articles