如何使用实体框架从数据库中检索数据 [英] How to retrieve data from database using entity framework

查看:87
本文介绍了如何使用实体框架从数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库表,包括{EmployeeId,name,city,gender}。我想检索它们并在视图中显示它..但是获得

的例外附加信息:在创建模型时不能使用上下文。如果上下文,则可能抛出此异常在OnModelCreating方法中使用,或者同时由多个线程访问相同的上下文实例。请注意,DbContext和相关类的实例成员不保证是线程安全的。



我的型号:



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel.DataAnnotations.Schema;
使用 System.Linq;
使用 System.Web;

命名空间 MvcPragim.Models
{
[表( tblEmployee)]
public class 员工
{

public int EmployeeId {获取; set ; }
public string 名称{ get ; set ; }
public string 性别{ get ; set ; }
public string 城市{ get ; set ; }

}
}
使用系统;
使用 System.Collections.Generic;
使用 System.Data.Entity;
使用 System.Linq;
使用 System.Web;

命名空间 MvcPragim.Models
{
public class EmployeeContext:DbContext
{
public DbSet< Employee>员工{获取; set ; }
}
}





我的控制器:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;
使用 MvcPragim.Models;

命名空间 MvcPragim.Controllers
{
public class EmployeeController:Controller
{
// 获取:员工
public ActionResult详细信息( int id)
{
EmployeeContext employeeContext = new EmployeeContext();
员工a​​Employee = employeeContext.Employees。(emp = > emp.EmployeeId == ID);
return 查看(aEmployee);
}
}
}





global.asax:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Data.Entity;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;
使用 System.Web.Optimization;
使用 System.Web.Routing;

命名空间 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);
}
}
}





这是一个重要的问题..帮帮我们。 。我怎么能摆脱它?Thnx提前

解决方案

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

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

如果您真的感觉很复杂,请查看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 


这篇关于如何使用实体框架从数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆