Repository navigation
Closed
Copy link
Description
In the following method, i got exception which i mentioned the title. But if i uncomment the following line it works.
// PreferenceIds = new List()
In which cases this type of exception may occur, any clue?
public static Expression<Func<ReservationGuest, GuestDto>> GuestSelector
{
get
{
return g => new GuestDto
{
Id = g.Id,
ProfileId = g.ProfileId,
VipLevelId = g.ProfileId.HasValue ? g.Profile.VipLevelId : g.ProfileId,
VipLevelCode = g.ProfileId.HasValue
? g.Profile.VipLevel.Code
: (g.VipLevelId.HasValue ? g.VipLevel.Code : null),
LanguageId = g.ProfileId.HasValue ? g.Profile.Language.Id : g.LanguageId,
LanguageName = g.ProfileId.HasValue
? g.Profile.Language.Name
: (g.Language != null ? g.Language.Name : string.Empty),
BirthDate = g.ProfileId.HasValue ? g.Profile.BirthDate : null,
IsOwner = g.IsOwner,
// PreferenceIds = new List<Guid>(), // works fine
PreferenceIds = g.ProfileId != null && g.Profile != null // exception
? g.Profile.Preferences.Select(p => p.PreferenceId).ToList()
: g.Preferences.Select(p => p.PreferenceId).ToList(),
FirstName = g.ProfileId.HasValue ? g.Profile.FirstName : g.FirstName,
LastName = g.ProfileId.HasValue ? g.Profile.LastName : g.LastName,
MiddleName = g.ProfileId.HasValue ? g.Profile.MiddleName : g.MiddleName,
PhoneCode = g.ProfileId.HasValue
? g.Profile.Communications
.Where(c => c.Type == CommunicationType.Phone && c.IsPreferred)
.Select(y => y.PhoneCode).FirstOrDefault()
: g.PhoneCode,
PhoneNumber = g.ProfileId.HasValue
? g.Profile.Communications
.Where(c => c.Type == CommunicationType.Phone && c.IsPreferred)
.Select(c => c.Value).FirstOrDefault()
: g.PhoneNumber,
Email = g.ProfileId.HasValue
? g.Profile.Communications
.Where(c => c.Type == CommunicationType.Email && c.IsPreferred)
.Select(c => c.Value).FirstOrDefault()
: g.Email
};
}
}Reacted by ZzZombo
Activity
suadev commentedon Mar 13, 2020
i found the following code in ProjectionMember.cs but absolutely no idea 🤒 @smitpatel
smitpatel commentedon Mar 15, 2020
The exception message is coming from internal implementation hence the message does not indicate what is wrong directly. Generally this error comes when looking for particular mapped projection which is not present. It is caused either by query is running in client and not appropriately handled to throw client eval exception or corrupt state during translation which is EF Core bug. Please provide runnable repro code which throws above exception so we can investigate root cause. Based on LINQ posted above, I am not able to see what would cause the error like this. Once we have identified root cause, we may be able to provide a work-around.
suadev commentedon Mar 15, 2020
It's really tough to create one self contained runnable repro but i'll try my best. Thank you
ajcvickers commentedon Mar 23, 2020
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.
arilishu commentedon Oct 5, 2020
hi @ALL..
Let me add my case here please.
public class Leads
{
public Leads()
{
DateCreated = DateTime.Now;
Hash = Helpers.CreateGuid(16);
CP = "";
States = new List();
Vehicles = new List();
Actions = new List();
}
[Key]
public int Id { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
public class Vehicles
{
public class Actions
{
public Actions()
{
DateCreated = DateTime.Now;
User = User;
}
[Key]
public int Id { get; set; }
[Required]
public DateTime DateCreated { get; set; }
[Required]
public string Message { get; set; }
[Required]
public Leads Lead { get; set; }
[Required]
public Users User { get; set; }
public class Policies
{
public Policies()
{
CreatedDate = DateTime.Now;
Active = true;
Comment = "";
}
[Key]
public int Id{ get; set; }
///
/// De qué vehiculo es la póliza?
///
public Vehicles Vehicle{ get; set; }
///
/// Ruta de la póliza en archivos
///
[MaxLength(500)]
public string Policy { get; set; }
public bool Active { get; set; }
///
/// Fecha desde que tiene cobertura el usuario
///
public DateTime? FromDate { get; set; }
///
/// Fecha que expira la póliza
///
public DateTime? ExpirationDate { get; set; }
public DateTime CreatedDate { get; set; }
///
/// Podemos llegar a agregar un comentario a la póliza
///
[MaxLength(500)]
public string Comment { get; set; }
///
/// Cobertura que tiene el usuario
///
public PoliciesCoverages? Coverage { get; set; }
///
/// Franquicia del auto en caso que sea Todo Riesgo
///
public int Franchise { get; set; }
///
/// Es la conversión de la póliza de su original a txt
///
public string? PolicyTXT { get; set; }
//Si es lead, no tiene categoría
public PoliciesCategories? Category { get; set; }
///
/// Si es cliente nuestro tiene quote, pero si es lead no.
///
public Quotes? Quote { get; set; }
///
/// Precio que paga cuando tomamos la póliza
///
[Column(TypeName = "money")]
public decimal Price { get; set; }
///
/// Si es cliente nuestro tiene quote, pero si es lead no.
///
[Column(TypeName = "money")]
public decimal lastQuotePrice { get; set; }
///
/// Si es cliente nuestro tiene sumaAsegurada, pero si es lead no.
///
[Column(TypeName = "money")]
public decimal lastQuoteSumAssured { get; set; }
public class LeadsIngresadosVM
{
public LeadsIngresadosVM()
{
Lead = new Leads();
Action = new Actions();
Vehicle = new Vehicles();
Policy = new Policies();
}
public Leads Lead{ get; set; }
public Actions Action { get; set; }
public Vehicles Vehicle { get; set; }
public Policies Policy { get; set; }
And the problem is:
var leads = db.Leads.Where(x => x.States.Count == 1).OrderByDescending(x => x.Id)
.Include(x => x.Actions).Include(x => x.Vehicles).ThenInclude(s => s.Policies)
.Select(x => new LeadsIngresadosVM
{
Lead = x,
Action = x.Actions.OrderByDescending(s => s.Id).FirstOrDefault(),
Vehicle = x.Vehicles.FirstOrDefault()
})
.ToList();
ajcvickers commentedon Oct 5, 2020
@arilishu The code posted is missing too much for me to be able to compile it without guessing many things. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
marcin-dardzinski commentedon Oct 23, 2020
I've noticed the same exception when I refer to the same owned collection twice within one projection and the owned entity itself has an owned type. If I remove the nested owned type from projection or if I refer to the child list only once it is fine. It is also fine if I replace
FirstOrDefaultwithToList.Here is the repro project: https://github.com/marcin-dardzinski/EFCoreProjectionRepro
10 remaining items
Create a provider agnostic test base to put custom models
Create a provider agnostic test base to put custom models (#24737)
alexdresko commentedon May 15, 2023
Here's the simplest possible repo, using the starting point: Getting Started - EF Core | Microsoft Learn
The following query throws the exception. Ignore the fact that this is a silly way to use EF. I'm just trying to narrow the problem down to its smallest repro, and this is what I came up with. It's the 2nd Select that causes the exception to be thrown.
roji commentedon May 16, 2023
@alexdresko this issue was closed two years ago; if you're still seeing this error, can you please open a new issue?
Webreaper commentedon May 14, 2024
Did anyone raise a new issue for this error? I can't find one, but it's still happening in .Net 8.
Khungersumit commentedon Aug 10, 2024
Okay, so here is the discussion about this issue. I am having the same issue.
query = groupBy(c=>some el)
Select(g => g.OrderByDescending(m => m.CreatedAt).FirstOrDefault())
// Culprit
query= query.OrderByDescending(c => c.CreatedAt);
This additional sorting operation seems to cause problems. Without this final sort, everything works as expected. I've spent hours trying to figure it out and even tried writing a raw SQL query, but that didn’t solve the problem either. I'm stumped! 🤔
nhuthan commentedon Aug 16, 2024
I am having the same problem on net 8