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 3962236
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:04:20+00:00 2026-05-20T03:04:20+00:00

I’ve been trying to solve this problem for a while now, and I’m at

  • 0

I’ve been trying to solve this problem for a while now, and I’m at a dead end, so maybe you guys can help. Note that this is not related to this question.

I am using Entity-Code First within MVC3. I must have done something dodgy to one of my objects, because when it first tries to create the database and populate it from my Seed function, this error occurs (the Seed function is never actually called, but the database and the tables are created).

The exception is thrown deep in the system:

>   mscorlib.dll!System.RuntimeType.CreateInstanceSlow(bool publicOnly, bool skipCheckThis, bool fillCache = true) + 0x63 bytes 
    mscorlib.dll!System.Activator.CreateInstance(System.Type type, bool nonPublic) + 0x46 bytes 
    System.Web.Mvc.dll!System.Web.Mvc.DependencyResolver.DefaultDependencyResolver.GetService(System.Type serviceType) + 0x25 bytes 
    System.Web.Mvc.dll!System.Web.Mvc.DependencyResolverExtensions.GetService<System.Web.Mvc.IControllerFactory>(System.Web.Mvc.IDependencyResolver resolver) + 0x3d bytes  
    System.Web.Mvc.dll!System.Web.Mvc.SingleServiceResolver<System.Web.Mvc.IControllerFactory>.Current.get() + 0x7e bytes   
    System.Web.Mvc.dll!System.Web.Mvc.MvcRouteHandler.GetSessionStateBehavior(System.Web.Routing.RequestContext requestContext = {System.Web.Routing.RequestContext}) + 0x72 bytes  
    System.Web.Mvc.dll!System.Web.Mvc.MvcRouteHandler.GetHttpHandler(System.Web.Routing.RequestContext requestContext = {System.Web.Routing.RequestContext}) + 0x2a bytes   
    System.Web.Mvc.dll!System.Web.Mvc.MvcRouteHandler.System.Web.Routing.IRouteHandler.GetHttpHandler(System.Web.Routing.RequestContext requestContext) + 0xb bytes 
    System.Web.dll!System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(System.Web.HttpContextBase context = {System.Web.HttpContextWrapper}) + 0x108 bytes  
    System.Web.dll!System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(object sender, System.EventArgs e) + 0x57 bytes 
    System.Web.dll!System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0x95 bytes   
    System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step = {System.Web.HttpApplication.SyncEventExecutionStep}, ref bool completedSynchronously = true) + 0x4c bytes    
    System.Web.dll!System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(System.Exception error) + 0x13e bytes  
    System.Web.dll!System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext context, System.AsyncCallback cb, object extraData) + 0xad bytes  
    System.Web.dll!System.Web.HttpRuntime.ProcessRequestInternal(System.Web.HttpWorkerRequest wr = {Microsoft.VisualStudio.WebHost.Request}) + 0x1a2 bytes  
    System.Web.dll!System.Web.HttpRuntime.ProcessRequestNoDemand(System.Web.HttpWorkerRequest wr) + 0x7d bytes  
    System.Web.dll!System.Web.HttpRuntime.ProcessRequest(System.Web.HttpWorkerRequest wr) + 0x47 bytes  
    WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Request.Process() + 0x17b bytes 
    WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Host.ProcessRequest(Microsoft.VisualStudio.WebHost.Connection conn = {System.Runtime.Remoting.Proxies.__TransparentProxy}) + 0x6c bytes 
    [Appdomain Transition]  
    WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Server.OnSocketAccept(object acceptedSocket) + 0x83 bytes   
    mscorlib.dll!System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(object state) + 0x2d bytes 
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes    
    mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() + 0x5a bytes 
    mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() + 0x147 bytes  
    mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() + 0x2d bytes    
    [Native to Managed Transition]  

I only have one object at the moment. Note, as I said earlier, it is created in the database:

// class Category
public class Category
{
    [Key]
    public int CategoryID { get; set; }

    [Required]
    [StringLength(64)]
    public string Name { get; set; }

    public int? ParentCategoryID { get; set; }
    [ForeignKey("ParentCategoryID")]
    public Category ParentCategory { get; set; }

    [Required]
    public int ListOrder { get; set; }

    // left/right
    public int TreeLeft { get; set; }
    public int TreeRight { get; set; }
}   // eo class Category

This is my DbContext-derived class:

// class ModelContext
public class ModelContext : DbContext
{
    public DbSet<Models.CMS.Category> ContentCategories { get; set; }

    // property to get the object context
    public ObjectContext ObjectContext
    {
        get
        {
            return ((IObjectContextAdapter)this).ObjectContext;
        }
    }


    // OnModelCreating
    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    {

        base.OnModelCreating(modelBuilder);
    }   // eo OnModelCreating

}   // eo class ModelContext

Not much going on there. And finally, my initializer. The Seed function is never called:

// class ModelInitializer
public class ModelInitializer : DropCreateDatabaseIfModelChanges<ModelContext>
{
    // seed
    protected override void Seed(ModelContext context)
    {
        var catRepo = Models.CMS.CategoryRepository.Instance;

        // Root category node
        context.ContentCategories.Add(catRepo.CreateRoot());

        // Test data
        Category home = catRepo.Add(new Category { Name = "Home", ListOrder = 10 });
        Category news = catRepo.Add(new Category { Name = "News", ListOrder = 20 });

        catRepo.Add(new Category { Name = "Current News", ListOrder = 10 }, news);
        catRepo.Add(new Category { Name = "Older News", ListOrder = 20 }, news);
    }   // eo Seed

}   // eo class ModelInitializer

You may or not require more information, and I will provide it obviously. I have no idea where to begin to look. The error might not even be related to my models, but anyway. I Note that it occurs in the MvcRouteHandler, I have not done anything special here and if I ignore the exception, I end up in my action handler for a HomeController. I get an error here because the database is not seeded (As Seed was never called).

Oh, and if it helps, the initializer in global.asax.cs:

    protected void Application_Start()
    {
        DbDatabase.SetInitializer<ModelContext>(new ModelInitializer());

        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

Any help or pointers would be appreciated!

EDIT: The controller looks like this:

    public ActionResult Index()
    {
        Models.CMS.CategoryRepository repo = Models.CMS.CategoryRepository.Instance;
        List<Models.CMS.Category> cats = repo.GetAll();
        return View();
    }

The error occurs before it gets to this handler, however. Some code in that GetAll() call then gets an error because the database is not populated (the Initializer was never called).

  • 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-20T03:04:21+00:00Added an answer on May 20, 2026 at 3:04 am

    I downloaded the project. First, to start fresh without any model issues, I set the ModelInitializer to inherit from DropCreateDatabaseAlways because I don’t want any stale data in the database each time I debug.

    When I ran the application, I was getting a null exception in the Add() method of the CategoryRepository. I think you may have a misunderstanding of the repository pattern. Currently, you’re using a static repository, which IMO is bad to do. You’re also creating a new model context in several places, which you should avoid by passing it in to the repository as a parameter. Ideally your interface for the Category repository should look something like this:

    public interface ICategoryRepository
    {
        void Add(Category category);
        void Remove(Category category);
        List<Category> GetAll(int parentId = 0);
        Category GetRoot();
        Category Get(int id);
    }
    

    Then inside your implementation of the repository would be something like this:

    public class CategoryRepository : ICategoryRepository
    {
        private readonly ModelContext context;
    
        // Here we pass in the context so that it can be used by methods.
        public CategoryRepository(ModelContext context)
        {
            this.context = context;
        }
    
        #region ICategoryRepository Members
    
        public void Add(Category category, Category parent = null)
        {
    
            if (parent == null)
            {
                parent = this.GetRoot();
            }
            // Snipped the stuff here.
    
            // Finally add to the current context.
            this.context.ContentCategories.Add(category);
        }
    
        // And all other methods...
    }
    

    I’ve made some significant changes to the project, so I hope they make sense for the repository pattern. You can find the updated project here. After I changed this, I was getting an exception about relationships – so I think your best be would be explain how you’re trying to model categories (some form of a tree structure…) and I can help you from there. Also, be sure to clean your project and make sure the database is empty. Cheers!

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.