如何使用实体框架 [英] How to use entity framework

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

问题描述

Hii,



i在类库中有模型类。

Hii ,

i have model class in class library.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MVCDemo.Models;

namespace MVCDemo.Class
{
    //This is Mapping to the table.
    [Table("Employees")]
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Title { get; set; }

        public Employee GetEmployeeDetailsById(int Id)
        {
            EmployeeContext _db = new EmployeeContext();
            var emp = (from em in _db.Employees.Where(e => e.EmployeeId == Id)
                       select new Employee
                       {
                           EmployeeId = em.EmployeeId,
                           FirstName = em.FirstName,
                           LastName = em.LastName,
                           Title = em.LastName
                       }).SingleOrDefault();
            return emp;

        }
    }
}







using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MVCDemo.Class;

namespace MVCDemo.Models
{
    //This class is conetxt class in enitty framework same as linq dbml context class

    //The Name Of this context class name should match to the name of the connection string in web.config file.
    public class EmployeeContext : DbContext
    {
        public DbSet<Employee> Employees { get; set; }
    }
}







这是我的模特: -






this is my model :-

using System.Web.Mvc;
using MVCDemo.Class;

namespace CopleteMVCDemoTutorials.Controllers
{
    public class EntityEmployeeController : Controller
    {
        //
        // GET: /EntityEmployee/

        public ActionResult GetEmployeeFromEntity(int Id)
        {
            //In Global.asax file added Database.SetInitializer<mvcdemo.models.employeecontext>(null);
           Employee objEmp=new Employee();
           objEmp = objEmp.GetEmployeeDetailsById(Id);
            return View(objEmp);
        }

    }
}





我的观点



my view

@model MVCDemo.Class.Employee

@{
    ViewBag.Title = "GetEmployeeFromEntity";
}

<h2>GetEmployeeFromEntity</h2>
<table border="1">
    <tr>
        <th>EmployeeId:</th>
        <td>@Model.EmployeeId</td>
    </tr>

    <tr>
        <th>FirstName:</th>
        <td>@Model.FirstName</td>
    </tr>

    <tr>
        <th>LastName:</th>
        <td>@Model.LastName</td>
    </tr>

    <tr>
        <th>Title:</th>
        <td>@Model.Title</td>
    </tr>
</table>



但是在获取记录的时候我得到了以下的错误..



类型


but at the time of fetching the record i am getting the below eror ..

An exception of type

'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code





其他信息:



Additional information:

The entity or complex type 'MVCDemo.Models.Employee' cannot be constructed in a LINQ to Entities query.

推荐答案

这里的问题是你试图在select语句中创建一个具有相同属性的新Employee,而LINQ to SQL不支持它。试着只提一下

The problem here is you are trying to create a new Employee with the same properties in the select statement, and that is not supported by LINQ to SQL. Try with only mentioning
select new
{

//The properties assgned goes here.
//
//

}





我们无法在里面创建实体一个LINQ。在这里,Employee是一个实体,在内部查询中,您正在创建新的Employee。

希望这会有所帮助。



We cannot create an entity inside a LINQ. That is here Employee is an entity and inside query you are creating new Employee.
Hope this helps.


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

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