简体   繁体   中英

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. 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.

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 = new Booking();
 booking.ExtraItemsVM = extralist.ToList();        

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. 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. For this, you need to force L2E to run the query prior to building your VM

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.

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. 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). A good question that can explain a lot about this is that one: The entity cannot be constructed in a LINQ to Entities query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM