我有这个模型
public class Appointment
{
[Key]
public Guid Id { get; set; }
public IEnumerable<Attendee> Attendees { get; set; }
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
}
和参加者课程
public class Attendee
{
public Guid AttendeeId { get; set; }
public Guid AppointmentId { get; set; }
public Appointment Appointment { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public Boolean IsApproved { get; set; }
public IEnumerable<Response> Responses { get; set; }
}
和DbContext类
public class DatePickerDbContext : IdentityDbContext
{
public DatePickerDbContext():base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("Users");
modelBuilder.Entity<ApplicationUser>().ToTable("Users");
modelBuilder.Entity<IdentityRole>().ToTable("Roles");
}
public DbSet<Appointment> Appointments { get; set; }
public DbSet<Attendee> Attendees { get; set; }
}
现在当我在Appointments表上进行查询时,例如
_db.Appointments.Find(appointmentId);
不应该懒得加载所有与会者吗?
或者我在模特中遗漏了一些东西?
PS:
EntityFramework版本:6.0.2
Asp.Net MVC版本:5.1.0
答案 0 :(得分:3)
要延迟加载相关的类,模型中的属性应该是virtual
,它应该是ICollection
,而不是IEnumerable
:
public virtual ICollection<Attendee> Attendees { get; set; }
答案 1 :(得分:1)
显然我应该改变
public IEnumerable<Attendee> Attendees { get; set; }
到
public virtual ICollection<Attendee> Attendees { get; set; }
PS:
简单地将其设置为虚拟IEnumerable<Attendee>
对我来说不起作用,必须是虚拟的ICollection<Attendee>