Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 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:

C#
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:

C#
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:

C#
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

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 
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900