Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6975231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:20:30+00:00 2026-05-27T17:20:30+00:00

I just started trying my hands on EF4 code first this morning and I

  • 0

I just started trying my hands on EF4 code first this morning and I created my POCO, data context and Initializer classes in a separate class library, I believe it’s the regular boiler plate type code. I reference the class in an MVC3 application and set the initializer in the Global.asax. On running the app, I notice the following problems
1. No database is created anywhere (Then I add an entry in the web.config for a connection string named after the Context class, still no result)
2. When I try to access the initalized values, I get a null error, obviously because there is no data.

Can anyone please help me with pointers on how to get thi thing to work (would be a shame if I spent my entire christmas day learning this and I still can’t get it to work 🙁 )
Thanks
p.s. I tried inserting break points and I hit the app initialization method, but it never hits the Seed method in the initializer even though I add a break point there as well!!
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 App 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);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:20:30+00:00Added an answer on May 27, 2026 at 5:20 pm

    Eureka..finally!
    While searching for a solution, I came across this post Entity Framework Database.SetInitializer simply not working
    Applying the solution suggested there to force my database to create worked at start up like I expected, but then while running the seed code, it threw a null pointer exception. While investigating, I realized that any attempt to reference the DBSet collections from the Context class yielded the same exception. Inspecting further I realised that instead of using

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

    I had used

       public DbSet<MobileTerminal> MobileTerminals;
    

    Which meant that I did not get any implicit object initialization, hence the null pointer exception. I removed the forced initialization code and ran the app again, this time the seed code didn’t run until I accessed a page which actually queried the data context and it ran perfectly.
    Apparently, due to Lazy loading, the initialization code is not run until it is actually needed, i.e. the first time the data context is queried in the application.

    I hope this helps anyone who has the same problem in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use this in my page class. I only just started
I have just started trying to build my first wp7 app, and I am
I just started trying out the mvc-mini-profiler in my MVC3 project with EF4.1 and
I've just started trying to integrate the Poco C++ Library with our game engine,
I've just started trying to learn JS so apologies if this seems like a
I've just started using NDepend and am trying to analyse a solution. This warning
I´ve just started learning GWT and I´m trying to implement http://gwt.google.com/samples/Showcase/Showcase.html#!CwFileUpload and but failing
I have just started learning Erlang and am trying out some Project Euler problems
I just started with Spring Web MVC. I'm trying to avoid file extenstions in
I just started working using Google Maps API yesterday, and trying to set up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.