I just started trying my hands with the EF4 code this morning, and I created my POCO classes, data and initializers in a separate class library, I believe that this is ordinary boiler plate type code. I reference the class in the MVC3 application and set the initializer in Global.asax. When I launch the application, I notice the following problems
1. No database is created anywhere (then I add an entry in the web.config file for the connection string named after the Context class, no result anyway)
2. When I try to access the initialized values, I get a zero error, obviously because there is no data.
Can someone please help me with pointers on how to get this work to work (it would be a shame if I spent all my Christmas day studying this and I still can't get it to work :() Thanks
postscript I tried to insert breakpoints and I hit the application initialization method, but it never hits the Seed method in the initializer, although I also add a breakpoint!
Thanks.
Initializer class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; using F2AController.Models; namespace F2AController.DataObjects { public class F2AInitializer : DropCreateDatabaseAlways<F2AContext> { protected override void Seed(F2AContext context) { var countries = new List<Country> { new Country(){ CountryName="Germany", Active = true}, new Country(){ CountryName="Britain", Active = true} }; countries.ForEach(s => context.Countries.Add(s)); context.SaveChanges(); var providers = new List<Providers>() { new Providers(){ ProviderName="InfoBip", ContactDetails="Rturo Manovic", Active=true, MessageRates= new List<ProviderRates>(){new ProviderRates(){ CountryId=1, DateCreated=DateTime.Now, DateModified=DateTime.Now, Rate=0.05M, Active=true}}} }; providers.ForEach(p => context.Providers.Add(p)); context.SaveChanges(); var usermobiles = new List<MobileTerminal>() { new MobileTerminal(){ Active= true, Credits=200, DateCreated=DateTime.Now, MSISDN="4477565444865"} }; usermobiles.ForEach(u => context.MobileTerminals.Add(u)); context.SaveChanges(); } } }
Context class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; namespace F2AController.Models { public class F2AContext : DbContext { public DbSet<Country> Countries; public DbSet<MobileTerminal> MobileTerminals; public DbSet<Providers> Providers; public DbSet<ProviderRates> ProviderRates; public DbSet<Property> Properties; public DbSet<ShortMessage> ShortMessages; public DbSet<UserProperties> UserProperties; protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } } }
Global.asax application initialization method
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); Database.DefaultConnectionFactory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["F2AContext"].ConnectionString); Database.SetInitializer<F2AContext>(new F2AInitializer()); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }