简体   繁体   中英

Entity Framework "FromSqlRaw or FromSqlInterpolated was called with non-composable SQL and with a query composing over it." error for a return object

I have this view

[Table("vw_Entity", Schema = "c")]
public partial class vw_Entity : BaseEntity
{
    public long? PredictedEntityTypeID { get; set; } 
    public bool IsManuallyChanged { get; set; } 
}

where BaseEntity is the class that stores only my ID and UUID .

This is my DTO return object:

public class EntityDTO
{
    public long ID { get; set; }
    public LookupTableDetails PredictedEntityTypeId { get; set; }
    public bool IsManuallyChanged { get; set; }
}

where LookupTableDetails looks like:

public class LookupTableDetails
{
    public long Id { get; set; }
    public string Name { get; set; }
}

Now I have this stored procedure that does basically a PATCH . I call it using the following snippet:

var data = await _context.vw_Entity.FromSqlRaw("EXECUTE core.Update_Entity @EntityID", parameters)
                .Select(x => new EntityDTO()
                {
                    ID = x.ID,
                    PredictedEntityTypeId = new LookupTableDetails() { Id = x.PredictedEntityTypeId, Name = x.PredictedEntityTypeId == 1 ? "Entity1" : "Entity2" },
                    IsManuallyChanged = x.IsManuallyChanged
                }).ToListAsync();

However, this crashes with an error

FromSqlRaw or FromSqlInterpolated was called with non-composable SQL and with a query composing over it

I'm aware what this error does, if I have a object of some other class inside my view then the stored procedure couldn't map it properly and return the error but in this case, my view is clear from obstacles of that type and all I need to do is just return the LookupTableDetails in my DTO object. The error is in

PredictedEntityTypeId = new LookupTableDetails() { Id = x.PredictedEntityTypeId, Name = x.PredictedEntityTypeId == 1 ? "Entity1" : "Entity2" }

I tried most of the solutions that the Internet offers, such as wrapping it with IgnoreFilters.. , AsEnumerable() etc.

Any ideas what is the cause and how can I prevent it from happening again in the future ie fix it? :D

Since You iterate over result from the first query, try changing to:

var data = _context.vw_Entity.FromSqlRaw("EXECUTE core.Update_Entity @EntityID", parameters)
                .ToList() // force change from IQueriable to IEnumerable and resolve result
                .Select(x => new EntityDTO()  //You are anyway using all results, so is ok
                {
                    ID = x.ID,
                    PredictedEntityTypeId = new LookupTableDetails() { Id = x.PredictedEntityTypeId, Name = x.PredictedEntityTypeId == 1 ? "Entity1" : "Entity2" },
                    IsManuallyChanged = x.IsManuallyChanged
                }).ToList();

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