且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用实体框架从数据库中检索数据

更新时间:2023-02-13 14:48:09

这个错误是一个非常常见的错误,它会在初始化时发生该模型。 

在您的情况下,如果您的现有表格和模型定义未匹配,则可能会发生这种情况。

如果您真的感觉很复杂,请查看http://www.codeproject.com/Articles/482801/DatabaseplusFirstplusDevelopmentpluswithplusASP-NE,为您现有的桌子创建一个模型


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

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