[英]How to do Many To Many relationship in Ef Core using Fluent API?
[英]How to do a 1 to many using LINQ and EF Core
在我的Place Repository中,我想帶上所有Place
記錄,包括每個Place
的任何Registration
記錄,即使Registration
表中沒有記錄。
目前我有:
public IEnumerable<Place> Places
{
get
{
return _appDbContext.Places.Include(r => r.Registrations.Where(q => q.PlaceId == r.Id));
}
}
導致以下錯誤:
InvalidOperationException: LINQ 表達式 '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")IdNull : objB. (object)EF,Property(r. "PlaceId"))).Where(r => r.PlaceId == r.Id)' 無法翻譯,要么以可翻譯的形式重寫查詢,或通過插入對“AsEnumerable”、“AsAsyncEnumerable”、“ToList”的調用顯式切換到客戶端評估。 或“ToListAsync”。 有關詳細信息,請參閱https://go.microsoft.com/fwlink/?linkid=2101038 。
Model:
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; }
}
只需將包含 function 更改為此
public IEnumerable<Place> Places
{
get
{
return _appDbContext.Places.Include(r => r.Registrations));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.