The entity cannot be constructed in a LINQ to Entities query

前端 未结 14 2030
失恋的感觉
失恋的感觉 2020-11-21 06:04

There is an entity type called Product that is generated by entity framework. I have written this query

public IQueryable GetProdu         


        
14条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 06:42

    In response to the other question which was marked as duplicate (see here) I figured out a quick and easy solution based on the answer of Soren:

    data.Tasks.AddRange(
        data.Task.AsEnumerable().Select(t => new Task{
            creator_id   = t.ID,
            start_date   = t.Incident.DateOpened,
            end_date     = t.Incident.DateCLosed,
            product_code = t.Incident.ProductCode
            // so on...
        })
    );
    data.SaveChanges();
    

    Note: This solution only works if you have a navigation property (foreign key) on the Task class (here called 'Incident'). If you don't have that, you can just use one of the other posted solutions with "AsQueryable()".

提交回复
热议问题