-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Selecting navigation properties results in System.InvalidOperationException: Operation is not valid due to the current state of the object #16719
Comments
I'm getting this error when executing using group by with navigation properties.
public class Role:IsSoftDelete
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<UserRole> UserRoles { get; set; }
}
public class UserRole: IsSoftDelete
{
public int UserId { get; set; }
public int RoleId { get; set; }
public Role Role { get; set; }
public User User { get; set; }
} // If I'm not grouping by g.Role.Name, It works
var query = _dairyContext.UserRoles
.Include(f => f.Role)
.GroupBy(g =>
new
{
RoleId = g.RoleId,
RoleName = g.Role.Name,
})
.Select(s => new
{
RoleId = s.Key.RoleId,
TotalUser = s.Sum(c => c.UserId),
RoleName = s.Key.RoleName
});
var result = await query.ToListAsync();
Further technical detailsEF Core version: 3.0.0-preview7.19365.7 It was working fine with preview 5 |
I have a similar error here. The same query works v2.2 with only one query generated and executed. |
I have a query that on preview6 was not throwing this exception and now on preview7 that is doing something really similar to the original issue (The original issue looks like a simpler example though). |
@smitpatel Full repro for the first issue; fails on current nightlies: public class GlobalSettings
{
public int Id { get; set; }
public Country DefaultCountryFkNavigation { get; set; }
}
public class Country
{
public int Id { get; set; }
}
public class BloggingContext : DbContext
{
public DbSet<GlobalSettings> GlobalSettings { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
public class Program
{
public static void Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}
using (var context = new BloggingContext())
{
context.GlobalSettings.Select(gs => new GlobalSettings
{
DefaultCountryFkNavigation = gs.DefaultCountryFkNavigation
}).FirstOrDefault();
}
}
}
|
@smitpatel Likewise for the second one: public class Role
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<UserRole> UserRoles { get; set; }
}
public class User
{
public int Id { get; set; }
}
public class UserRole
{
public int UserId { get; set; }
public int RoleId { get; set; }
public Role Role { get; set; }
public User User { get; set; }
}
public class BloggingContext : DbContext
{
public DbSet<UserRole> UserRoles { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserRole>().HasKey(e => new { e.UserId, e.RoleId });
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
public class Program
{
public static async Task Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}
using (var context = new BloggingContext())
{
var query = context.UserRoles
.Include(f => f.Role)
.GroupBy(g =>
new
{
RoleId = g.RoleId,
RoleName = g.Role.Name,
})
.Select(s => new
{
RoleId = s.Key.RoleId,
TotalUser = s.Sum(c => c.UserId),
RoleName = s.Key.RoleName
});
var result = await query.ToListAsync();
}
}
}
|
Case 1: [ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Navigation_inside_MemberInitExpression_is_expanded(bool isAsync)
{
return AssertQuery<Order>(
isAsync,
os => os.Where(o => o.OrderID < 10300).Select(o =>
new OrderDTO
{
Customer = o.Customer
}),
entryCount: 221);
}
private class OrderDTO
{
public Customer Customer { get; set; }
} The custom expression there is |
Case 2: [ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task TestingSomething(bool isAsync)
{
return AssertQuery<Order>(
isAsync,
os => os.Include(o => o.Customer)
.GroupBy(o => new { o.OrderID, o.Customer.City })
.Select(g => new { g.Key.OrderID, C = g.Sum(o => o.OrderID), g.Key.City }),
entryCount: 221);
} Navigation inside KeySelector is not expanded from nav expansion hence translation fails. |
Split off the first case (entity equality) to #16789, let's keep this to track the second (navigation). |
@smitpatel Should this be in 3.0? |
Duplicate of #15249 |
just hit this with 3.0 preview 7 and also 8. was ok in 6. My query is simple though no group by just context.DbSet.Include(d=>d.NavigationCollectionProperty) |
Selecting navigation properties into a named class results in the following exception
Steps to reproduce
Points of importance :
GlobalSettings
is a scaffolded Model from EFnew { }
doesn't throwLet me know if you need a full repro, but the above code is basically all you need.
Further technical details
EF Core version: 3.0.0-preview7.19365.7
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 x64
IDE: Visual Studio 2019 16.2.0 Preview 4.0
The text was updated successfully, but these errors were encountered: