C
C#2y ago
Dropps

✅ System.NotSupportedException with EF Core

quick question:
public async Task<List<Order>> GetAllOrders()
{
return await _dbContext.Orders
.Include(o => o.OrderFoods!)
.ThenInclude(of => of.Food)
.ToListAsync();
}
public async Task<List<Order>> GetAllOrders()
{
return await _dbContext.Orders
.Include(o => o.OrderFoods!)
.ThenInclude(of => of.Food)
.ToListAsync();
}
gives me a NotSupportedException but how do i fix that? dataclass:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

#pragma warning disable CS8618
namespace iFoodTracker.Models;

public class Order
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public DateTime CreatedAt { get; set; }
public bool PaidStatus { get; set; }
public string UserId { get; set; }
public string? EmployeeShort { get; set; }
public IEnumerable<OrderFood>? OrderFoods { get; set; }
}
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

#pragma warning disable CS8618
namespace iFoodTracker.Models;

public class Order
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public DateTime CreatedAt { get; set; }
public bool PaidStatus { get; set; }
public string UserId { get; set; }
public string? EmployeeShort { get; set; }
public IEnumerable<OrderFood>? OrderFoods { get; set; }
}
15 Replies
Jimmacle
Jimmacle2y ago
what are the details of the exception? i suspect it's because you modeled your navigation as IEnumerable<T> when EF requires a type implementing ICollection<T>
Dropps
DroppsOP2y ago
give me a second i have a hard time already getting the inner exception as its only shown in debugger but not triggerd by a try catch the inner exception equals to "null"
Dropps
DroppsOP2y ago
Saber
Saber2y ago
Try an ICollection or List instead of IEnumerable as the navigation property.
Dropps
DroppsOP2y ago
do i need to remigrate the db for that?
Saber
Saber2y ago
i doubt it
Dropps
DroppsOP2y ago
then i have the same error with the ICollection too
Saber
Saber2y ago
that screenshot from before doesn't look like you are inspecting an exception, it looks like you are trying to inspect the dbset, which then is giving an exception when you access a certian property within it
Jimmacle
Jimmacle2y ago
yeah, pretty sure the red highlight means it's an error coming from the IDE
Dropps
DroppsOP2y ago
well otherwise i dont get any exception and the frontend just freezes
Jimmacle
Jimmacle2y ago
try to make a minimum reproduction of the issue get rid of the async, test the database access by itself instead of through your repository/frontend/etc if it starts working, add things back one by one until it breaks again
Dropps
DroppsOP2y ago
i wouldnt even know where to start that as the other tables work i probs gonna refactor the entire db access stuff once more and just screw everything over
Jimmacle
Jimmacle2y ago
or you could do what i suggested take a methodical approach to reducing the surface area to check for bugs
Dropps
DroppsOP2y ago
i found it out now my entire db just had with 5 tables the ability to create 23 circular references XD
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server