如何使用 LINQ 和 EF Core 实现一对多
How to do a 1 to many using LINQ and EF Core
在我的 Place Repository 中,我想带来所有 Place
记录,包括每个 Place
的任何 Registration
记录,即使有Registration
table.
中没有记录
目前我有:
public IEnumerable<Place> Places
{
get
{
return _appDbContext.Places.Include(r => r.Registrations.Where(q => q.PlaceId == r.Id));
}
}
导致以下错误:
InvalidOperationException: The LINQ expression 'DbSet() .Where(r => EF.Property(EntityShaperExpression: EntityType: Place ValueBufferExpression: ProjectionBindingExpression: Outer.Outer.Outer.Outer.Outer.Outer.Outer.Outer IsNullable: False , "Id") != null && object.Equals( objA: (object)EF.Property(EntityShaperExpression: EntityType: Place ValueBufferExpression: ProjectionBindingExpression: Outer.Outer.Outer.Outer.Outer.Outer.Outer.Outer IsNullable: False , "Id"), objB: (object)EF.Property(r, "PlaceId"))) .Where(r => r.PlaceId == r.Id)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
模特:
public class Place
{
private const int _annualFiguresDueWithinDays = 90;
public int Id { get; set; }
[Required(ErrorMessage = "Please enter in the Place's name.")]
[MaxLength(100, ErrorMessage = "The maximum length for the place name is 100 characters.")]
public string PlaceName { get; set; }
public IEnumerable<Registration> Registrations { get; set; }
}
public class Registration
{
public int Id { get; set; }
[ForeignKey("PlaceId")]
public int PlaceId { get; set; }
public DateTime? RegistrationDate { get; set; }
public bool IsFirstRegistration { get; set; }
}
把include函数改成这样就可以了
public IEnumerable<Place> Places
{
get
{
return _appDbContext.Places.Include(r => r.Registrations));
}
}
在我的 Place Repository 中,我想带来所有 Place
记录,包括每个 Place
的任何 Registration
记录,即使有Registration
table.
目前我有:
public IEnumerable<Place> Places
{
get
{
return _appDbContext.Places.Include(r => r.Registrations.Where(q => q.PlaceId == r.Id));
}
}
导致以下错误:
InvalidOperationException: The LINQ expression 'DbSet() .Where(r => EF.Property(EntityShaperExpression: EntityType: Place ValueBufferExpression: ProjectionBindingExpression: Outer.Outer.Outer.Outer.Outer.Outer.Outer.Outer IsNullable: False , "Id") != null && object.Equals( objA: (object)EF.Property(EntityShaperExpression: EntityType: Place ValueBufferExpression: ProjectionBindingExpression: Outer.Outer.Outer.Outer.Outer.Outer.Outer.Outer IsNullable: False , "Id"), objB: (object)EF.Property(r, "PlaceId"))) .Where(r => r.PlaceId == r.Id)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
模特:
public class Place
{
private const int _annualFiguresDueWithinDays = 90;
public int Id { get; set; }
[Required(ErrorMessage = "Please enter in the Place's name.")]
[MaxLength(100, ErrorMessage = "The maximum length for the place name is 100 characters.")]
public string PlaceName { get; set; }
public IEnumerable<Registration> Registrations { get; set; }
}
public class Registration
{
public int Id { get; set; }
[ForeignKey("PlaceId")]
public int PlaceId { get; set; }
public DateTime? RegistrationDate { get; set; }
public bool IsFirstRegistration { get; set; }
}
把include函数改成这样就可以了
public IEnumerable<Place> Places
{
get
{
return _appDbContext.Places.Include(r => r.Registrations));
}
}