2
我正在用ASP.NET Core創建一個新項目&實體框架核心。 (我認爲這是實體框架7?)使用實體框架核心加載相關數據
我想MS已經刪除了延遲加載,所以我的虛擬集合不會自動加載。所以然後我決定嘗試「.include」,但是這種方法不見了。所以我包括「System.Data.Entity」,現在我有.include,但它仍然加載集合爲null。實體必須在數據庫中有相關數據。
我的代碼如下:
//Property Table
public class Property
{
public int Id { get; set; }
public string PropertyName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Postcode { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string PhoneNumber { get; set; }
public virtual ICollection<BodyCorpMember> BodyCorpMembers { get; set; }
public bool Deleted { get; set; }
}
//Body Corp Table
public class BodyCorpMember
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public bool IsSecretary { get; set; }
public int Property_Id { get; set; }
[ForeignKey("Property_Id")]
public virtual Property Property { get; set; }
}
這是一些我在OnModelCreating嘗試沒有成功 //builder.Entity() // .HasOne(A => a.Property的事情) // .WithMany(a => a.BodyCorpMembers) // .HasForeignKey(「Property_Id」);
//builder.Entity<Property>()
// .HasMany(a => a.BodyCorpMembers)
// .WithOne(a => a.Property);
//Here is my main problem, Ive tried multiple options for ".include" but it always returns null for BodyCorpMembers
property = _dbContext.Properties.Include(a=>a.BodyCorpMembers).Where(a=>a.Id ==id).FirstOrDefault();
還應該注意的是,我使用內置的IdentityDbContext作爲我的Db上下文。即:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
以下內容是否返回null? '_dbContext.Properties.Include(a => a.BodyCorpMembers) .FirstOrDefault(a => a.BodyCorpMembers.ToList()。Count> 0)' –
[Entity Framework Core - Lazy Loading](https ://stackoverflow.com/questions/40122162/entity-framework-core-lazy-loading) –