Click here to Skip to main content
14,236,057 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a database table consists of {EmployeeId,name,city,gender}. i want to retrieve them and show it in the view..but getting the exception of
"Additional information: The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe."

My model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace MvcPragim.Models
{
    [Table("tblEmployee")]
    public class Employee
    {
       
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }

    }
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace MvcPragim.Models
{
    public class EmployeeContext:DbContext
    {
        public DbSet<Employee> Employees { get; set; }
    }
}


my controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPragim.Models;

namespace MvcPragim.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Details(int id)
        {
            EmployeeContext employeeContext=new EmployeeContext();
            Employee aEmployee= employeeContext.Employees.Single(emp => emp.EmployeeId == id);
            return View(aEmployee);
        }
    }
}


global.asax:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MvcPragim
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            Database.SetInitializer<MvcPragim.Models.EmployeeContext>(null);
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}


It's a an important issue..Help me out guys..How can i get rid of it?Thnx in advance
Posted
Updated 16-Mar-17 6:24am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

This error is a very common error which will occur at the time of initialising the model.

in your case it might have occurred if your existing table and the model definition have not matched check on to it.

If you really feel it complex check on to this "http://www.codeproject.com/Articles/482801/DatabaseplusFirstplusDevelopmentpluswithplusASP-NE" which creates a model for your existing table 
   
Comments
star_tasneem 28-Mar-15 1:47am
   
i have a new error now. it is " The underlying provider failed on Open."
ramyajaya 28-Mar-15 9:40am
   
Please add more details about the scenario in which it occur

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100