Entity Framework Element Code First in the Class Library

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); } 
+4
source share
3 answers

Eureka..finally!
When searching for a solution, I came across this Entity Framework Database.SetInitializer message just doesn't work
Applying the solution proposed there to force my database to create created at startup, as I expected, but then when running the seed code, he threw a null pointer exception. When researching, I realized that any attempt to reference the DBSet collections from the Context class gave the same exception. Looking further, I realized that instead of using

  public DbSet<MobileTerminal> MobileTerminals { get; set; } 

I used

  public DbSet<MobileTerminal> MobileTerminals; 

This meant that I did not receive any implicit initialization of the object, hence the null pointer exception. I removed the forced initialization code and ran the application again, this time the initial code did not start until I got access to the page that actually requested the data context, and it worked fine.
Apparently, due to Lazy loading, the initialization code does not run until it is needed, that is, the first time the data context is requested in the application.

I hope this helps anyone with the same problem in the future.

+4
source

I wanted to share another problem when I first used the class library for code and came across this entry. I had my first POCO and DataContext class code in a library project, and I wanted to use this project to create the first database of my code. I realized that the -ProjectName flag exists, with which you can specify the class library project to look for when creating the database.

  add-migration -Name 'InitialCreate' -ProjectName 'MyProject.Data' update-database -ProjectName 'MyProject.Data' 
+1
source

The problem may be using the connection string you use in your web.config.

For SQL CE, use the following

 <add name="YourContext" connectionString="Data Source=|DataDirectory|yourDB.sdf" providerName="System.Data.SqlServerCe.4.0"/> 

For SQL Express use

 <add name="YourContext" connectionString="Data source=.\SQLEXPRESS;Integrated Security=True;Initial Catalog=YourDatabase;" providerName="System.Data.SqlClient" /> 

I think this should make everything work.

Also, I think you should look at this article Initializing the First EF Code Code Using Web.Config . It is better to initialize the database from web.config, and not from the global.asax file

0
source

Source: https://habr.com/ru/post/1387953/


All Articles