简体   繁体   English

Asp.Net MVC C#其他信息:实体或复杂类型不能在LINQ to Entities查询中构造

[英]Asp.Net MVC C# Additional information: The entity or complex type cannot be constructed in a LINQ to Entities query

I get this error: The entity or complex type 'ebs.Models.ExtraItemVM' cannot be constructed in a LINQ to Entities query. 我收到此错误: The entity or complex type 'ebs.Models.ExtraItemVM' cannot be constructed in a LINQ to Entities query. on this line: 在这条线上:

 booking.ExtraItemsVM = extralist.ToList(); 

I've seen other examples of this, where it says you get this error if you try to map to actual database tables. 我看过其他示例,它说如果您尝试映射到实际的数据库表,则会出现此错误。

In my case, I'm trying to map to a ViewModel. 就我而言,我试图映射到ViewModel。

My controller code is below. 我的控制器代码如下。

Firstly get a list of "extras" from the database: 首先从数据库中获取“附加”列表:

 // get a list of extras
 var extralist = db.Extras.Where(x => x.hotel_id == AccID)
    .Select(e => new ExtraItemVM
     {
      eID = e.additem_id,
      Description = e.additem_text,
      Price = e.additem_cost,
      Count = 0
     });

Then create a new "booking" view model, and try to attach the list of extras to the Booking.ExtraItemsVM property fails: 然后创建一个新的“ booking”视图模型,并尝试将其他列表附加到Booking.ExtraItemsVM属性失败:

 Booking booking = new Booking();
 booking.ExtraItemsVM = extralist.ToList();        

ViewModels* *的ViewModels

 public class Booking
 {
    public int ID { get; set; }
    public bool HasResults { get; set; }
    public List<ExtraItemVM> ExtraItemsVM { get; set; }
  ...
  ...
 }

public class ExtraItemVM
{
    public int ID { get; set; }
    public long eID { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public int Count { get; set; }
}

Database Model 数据库模型

public class Extra
{
    [Key]
    [Display(Name = "ID")]
    public long additem_id { get; set; }
    public long hotel_id { get; set; }
    public string additem_text { get; set; }
    public decimal additem_cost { get; set; }
 }

I don't think I'm mapping to the database table - so can anyone please help show what I've done wrong please? 我不认为我正在映射到数据库表-所以任何人都可以帮助显示我做错了什么吗?

Thank you, 谢谢,

Mark 标记

I don't have enough information to replicate this problem, but I have a serious feeling that it is the following section of code. 我没有足够的信息来重复这个问题,但是我有一种严重的感觉,那就是下面的代码部分。

var extralist = db.Extras.Where(x => x.hotel_id == AccID)
   .Select(e => new ExtraItemVM
    {
     eID = e.additem_id,
     Description = e.additem_text,
     Price = e.additem_cost,
     Count = 0
    });

The easiest way to debug this might be to eliminate the Select clause or the Where clause and see if that eliminates your problem. 调试此问题的最简单方法可能是消除Select子句或Where子句,然后查看是否可以消除您的问题。 You could always do this in a couple steps instead of trying to do it in one fell swoop. 您总是可以分几步来完成此操作,而不是一口气尝试一下。

In a case like this we would need more info to really find the root cause. 在这种情况下,我们需要更多信息才能真正找到根本原因。 The first two things I would try in order to debug that would be. 我将尝试调试的前两件事是。

1) Verifying that the query (where clause) doesn't cause any problem. 1)验证查询(where子句)没有引起任何问题。 For this, you need to force L2E to run the query prior to building your VM 为此,您需要在构建虚拟机之前强制L2E运行查询

var extralist = db.Extras.Where(x => x.hotel_id == AccID)
                  .AsEnumerable()  //This will force L2E to execute the query
                  .Select(e => new ExtraItemVM
                         {
                              eID = e.additem_id,
                              Description = e.additem_text,
                              Price = e.additem_cost,
                              Count = 0
                         });

2) The second option would be to remove the VM class and to use an anonymous class. 2)第二种选择是删除VM类并使用匿名类。

var extralist = db.Extras.Where(x => x.hotel_id == AccID)
                  .Select(e => new ()
                         {
                              eID = e.additem_id,
                              Description = e.additem_text,
                              Price = e.additem_cost,
                              Count = 0
                         })
                  .AsEnumerable();

If the second option works, then your VM isn't legal for L2E. 如果第二个选项有效,则您的VM不适用于L2E。 In order to build a class inside of a Select statement, you must use a class with a default constructor (just as in your sample code). 为了在Select语句中构建类,您必须使用具有默认构造函数的类(就像示例代码中一样)。 A good question that can explain a lot about this is that one: The entity cannot be constructed in a LINQ to Entities query 一个可以解释很多问题的好问题是: 不能在LINQ to Entities查询中构造实体

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#-无法在LINQ to Entities查询中构造实体或复杂类型 - C# - The entity or complex type cannot be constructed in a LINQ to Entities query C#MVC Linq子查询不能在LINQ to Entities查询中构造实体或复杂类型“ XXX” - C# MVC Linq subquery The entity or complex type 'XXX' cannot be constructed in a LINQ to Entities query 附加信息:不能在LINQ to Entities查询中构造实体或复杂类型&#39;cb2.Models.ResultsVM&#39; - Additional information: The entity or complex type 'cb2.Models.ResultsVM' cannot be constructed in a LINQ to Entities query I'm new to asp.net mvc and entity Jquery ajax error says 'The entity or complex type 'myModel'' cannot be constructed in a LINQ to Entities query.' - I'm new to asp.net mvc and entity Jquery ajax error says 'The entity or complex type 'myModel'' cannot be constructed in a LINQ to Entities query.' 无法在 LINQ to Entities 查询中构造实体或复杂类型“ ” - The entity or complex type ' ' cannot be constructed in a LINQ to Entities query 实体或复杂类型&#39;x&#39;不能在linq to实体查询中构造 - the entity or complex type 'x' cannot be constructed in a linq to entities query 实体或 &gt; 复杂类型“ChildAccessory”不能在 LINQ 中构造到 &gt; 实体查询 - The entity or > complex type 'ChildAccessory' cannot be constructed in a LINQ to > Entities query 实体或复杂类型……无法在LINQ to Entities查询中构造 - The entity or complex type … cannot be constructed in a LINQ to Entities query 收到错误:无法在LINQ to Entities查询中构造实体或复杂类型 - Getting error:The entity or complex type cannot be constructed in a LINQ to Entities query 实体或复杂类型不能在LINQ to Entities查询中构造 - The entity or complex type cannot be constructed in a LINQ to Entities query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM