我正在使用mvc 4.5并尝试创建Events页面,每个Event都会有Event Attendees列表;我添加了类MyEvent和类Attendee
public class MyEvent
{
public MyEvent()
{
Attendees = new List<Attendee>();
}
[Required]
public int Id { get; set; }
[Required]
[Display(Name = "Date")]
public DateTime EventDate { get; set; }
[Required]
[Display(Name = "Time")]
public string EventTime{ get;set;}
[Required]
[Display(Name = "Event Name")]
public string Name{ get; set; }
[Required]
[Display(Name = "Event Place")]
public string Address { get; set; }
public List<Attendee> Attendees { get; set; }
[Required]
[Display(Name = "Description")]
public string Description{ get; set; }
[Required]
[Display(Name = "Host")]
public string Host { get; set; }
}
public class Attendee
{
public int Id { get; set; }
public string Name { get; set; }
public Attendee(String name)
{
this.Name = name;
}
}
EF创建了表MyEvents,Attendees和Attendees的Fk MyEvent_Id指向了MyEvent表的Id;问题我无法在代码中访问MyEvent_Id,而我可以访问其余列(Id和Name),另一个问题是MyEvent Entity的Attendees列表似乎一遍又一遍地更新并且只包含最后一个attndee;我希望能够让所有与活动相关的与会者都能够显示所有活动参与者
答案 0 :(得分:0)
听起来您正在尝试从参加者访问MyEvent对象,但是您缺少导航属性来访问您的事件对象。您的参加者类应该看起来像这样
public class Attendee
{
public int Id { get; set; }
public string Name { get; set; }
public int MyEventID { get; set; }
public virtual MyEvent MyEvent { get; set; }
public Attendee(String name)
{
this.Name = name;
}
}
你的第二个问题对我来说并不是很清楚。实际框架通常仅在您实际访问实体时从数据库中读取参加者。您应该能够毫无问题地遍历参加者列表。