Entity Framework 脚手架及相关资料
Entity Framework scaffold and related data
我正在构建一个简单的应用程序来学习 Entity Framework,大致遵循 this and this 教程。
我已经成功地以代码优先的方式创建了我的表,我可以查询它们并查看预期数据以及与我的种子数据的一对多关系的正确键。
public class Organization
{
public Organization()
{
Members = new HashSet<Member>();
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Member> Members { get; set; }
}
public class Member
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
public string Name { get; set; }
public string Note { get; set; }
public Guid OrganizationID { get; set; }
public virtual Organization Organization { get; set; }
}
public class OrganizationMemberContext : DbContext
{
public OrganizationMemberContext(DbContextOptions<OrganizationMemberContext> options) : base(options)
{
}
public DbSet<Organization> Organizations { get; set; }
public DbSet<Member> Members { get; set; }
}
我继续使用 OrganizationMemberContext 为 Organization
模型创建一个脚手架控制器,并且效果很好。 CRUD 操作似乎按预期运行。
然后我想在索引页上显示每个组织在该组织下的成员列表。然而...我认为应该有效returns 没有相关数据。
public async Task<IActionResult> Index()
{
_context.Organizations.Include(x => x.Members);
return View(await _context.Organizations.ToListAsync());
}
我得到了组织列表,但是每个组织的成员 属性 在发送到页面的模型中是空的(确认在 VS 中观看本地人)。
同样,种子数据被正确插入:我可以在数据库中看到 Organization.ID 值与预期的 Member.OrganizationID 值精确匹配。
原码
public async Task<IActionResult> Index()
{
_context.Organizations.Include(x => x.Members); // use include with no additional queries
return View(await _context.Organizations.ToListAsync()); // made a new query - it knows nothing about include part - Lazy loading still in effect
}
应该是:
public async Task<IActionResult> Index()
{
return View(await _context.Organizations.Include(x => x.Members)ToListAsync()); // query see include and add this information in the result
}
我正在构建一个简单的应用程序来学习 Entity Framework,大致遵循 this and this 教程。
我已经成功地以代码优先的方式创建了我的表,我可以查询它们并查看预期数据以及与我的种子数据的一对多关系的正确键。
public class Organization
{
public Organization()
{
Members = new HashSet<Member>();
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Member> Members { get; set; }
}
public class Member
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
public string Name { get; set; }
public string Note { get; set; }
public Guid OrganizationID { get; set; }
public virtual Organization Organization { get; set; }
}
public class OrganizationMemberContext : DbContext
{
public OrganizationMemberContext(DbContextOptions<OrganizationMemberContext> options) : base(options)
{
}
public DbSet<Organization> Organizations { get; set; }
public DbSet<Member> Members { get; set; }
}
我继续使用 OrganizationMemberContext 为 Organization
模型创建一个脚手架控制器,并且效果很好。 CRUD 操作似乎按预期运行。
然后我想在索引页上显示每个组织在该组织下的成员列表。然而...我认为应该有效returns 没有相关数据。
public async Task<IActionResult> Index()
{
_context.Organizations.Include(x => x.Members);
return View(await _context.Organizations.ToListAsync());
}
我得到了组织列表,但是每个组织的成员 属性 在发送到页面的模型中是空的(确认在 VS 中观看本地人)。
同样,种子数据被正确插入:我可以在数据库中看到 Organization.ID 值与预期的 Member.OrganizationID 值精确匹配。
原码
public async Task<IActionResult> Index()
{
_context.Organizations.Include(x => x.Members); // use include with no additional queries
return View(await _context.Organizations.ToListAsync()); // made a new query - it knows nothing about include part - Lazy loading still in effect
}
应该是:
public async Task<IActionResult> Index()
{
return View(await _context.Organizations.Include(x => x.Members)ToListAsync()); // query see include and add this information in the result
}