无法使用linq访问实体数据错误:无法在实体查询中构造实体或复杂类型 [英] Unable to access data using linq to entity Error: The Entity Or Complex Type Cannot Be Constructed In A Linq To Entities Query

查看:215
本文介绍了无法使用linq访问实体数据错误:无法在实体查询中构造实体或复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.NET MVC和Linq to Entity的新手。我正在开发一个具有4个业务层的应用程序:UI,业务逻辑,DAL和服务层。我编写了一个测试逻辑但它给了我以下错误:

 **无法在Linq To Entities查询中构造实体或复杂类型。** 



** DAL中的实体查询LInq(命名存储库):**

  public 列表< PROGNOSI> GetData( int  id)
{
使用(CAAEntities context = new CAAEntities())
{
var query = 来自 p context.PROGNOSIs
其中​​ p.ID == id
选择 new {ID = p.ID,NAME = p.NAME};

var data = query.ToList()。选择(r = > new PROGNOSI
{
ID = r.ID,NAME = r.NAME
})。ToList();

返回数据;

}
}



然后我在UI(CAA_AuditPlanning)层的控制器功能中使用上述方法:



  public  ActionResult Prognosi()
{
< span class =code-keyword> var aeromedicalRepository = new AeroMedicalRepository();
列表< PROGNOSI> data = aeromedicalRepository.GetData( 1 ); // 此处出现错误

返回查看(数据);

}



**查看:**

 @ model IEnumerable < span class =code-keyword><   Repositories.PROGNOSI  >  

< div class = col-lg-8 >
< table class = 报告过滤器 >
@foreach(模型中的var项目)
{

< tr >
< td > @ Html.DisplayFor(m => item.NAME)< / td >
< / tr >
}
< / table >
< / div >



**型号类别:**

  public   class  Prognosi 
{
public string ID { get ; set ; }

[必需]
[显示(名称= 预测标题)]
[StringLength( 150 ,ErrorMessage = 标题不能超过150个字符。)]
public string 名称{获取; set ; }

}



谁能告诉我我的代码有什么问题?



注意:如果我不使用Repositories Layer并直接在控制器函数中编写GetData逻辑,那么代码工作正常。显然,在传递方法结果时会出现问题。

解决方案

此错误消息表示您无法将查询结果作为映射实体返回。您必须返回匿名类型。



尝试简化 GetData 功能如下:



 使用(CAAEntities context =  new  CAAEntities())
{
var query = context.PROGNOSIs
.Where(p => p.ID = = id)
。选择(p => new {ID = p.ID,NAME = p.NAME})
.ToList();

return 查询;
}





如需了解更多信息,请参阅:

错误:实体或复杂类型无法在LINQ to Entities查询中构建

实体或复杂类型''不能在LINQ to Entities查询[duplicate] 中构建


I am a novice in both ASP.NET MVC and Linq to Entity. I am developing an application that has 4 business layers: UI, Business logic, DAL and service layer. I have written a test logic but it is giving me the following error:

**The Entity Or Complex Type Cannot Be Constructed In A Linq To Entities Query.**


**LInq to Entity query in DAL(named Repositories):**

public List<PROGNOSI> GetData(int id)
        {
            using (CAAEntities context = new CAAEntities())
            {
                var query = from p in context.PROGNOSIs
                where p.ID == id
                select new {ID = p.ID, NAME = p.NAME };

                var data = query.ToList().Select(r => new PROGNOSI
                {
                     ID = r.ID, NAME= r.NAME
                }).ToList();

                return data;

            }
}


Then I am using the above method in controller function in UI(CAA_AuditPlanning) layer as:

public ActionResult Prognosi()
        {
           var aeromedicalRepository = new AeroMedicalRepository();
           List<PROGNOSI> data = aeromedicalRepository.GetData(1); //here the error comes

           return View(data);

        }


**View:**

@model  IEnumerable<Repositories.PROGNOSI>

<div class="col-lg-8">
        <table class="Reports-filter">
@foreach (var item in Model)
                {

            <tr>
                <td>@Html.DisplayFor(m => item.NAME)</td>
            </tr>
}
        </table>
    </div>


**Model Class:**

public class Prognosi
{
    public string ID { get; set; }

    [Required]
    [Display(Name = "Prognosis Title")]
    [StringLength(150, ErrorMessage = "Title cannot be longer than 150 characters.")]
    public string NAME { get; set; }

}


Can anyone tell me what is wrong with my code?

Note: If I do not use Repositories Layer and write GetData logic directly in controller function then code works fine. Apparently, the problem arises in passing method results.

解决方案

This error message means that you can't return result of query as mapped entity. You have to return anonymous type.

Try to simplify GetData function as follow:

using (CAAEntities context = new CAAEntities())
{
    var query = context.PROGNOSIs
        .Where(p=>p.ID == id)
        .Select(p=>new{ID = p.ID, NAME = p.NAME})
        .ToList();

    return query;
}



For further information, please see:
Error: The entity or complex type cannot be constructed in a LINQ to Entities query
The entity or complex type ' ' cannot be constructed in a LINQ to Entities query [duplicate]


这篇关于无法使用linq访问实体数据错误:无法在实体查询中构造实体或复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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