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) –