我一直在为实体框架使用 Code First 方法。我有一个 Event 类、一个 Band 类和一个 EventBands 类,它们映射了多对多关系。Code First 方法运行良好(当我没有 EventBands 类时),但后来我决定我想要多对多表来存储附加值。现在我收到此错误消息:
System.Data.Edm.EdmEntityType: : EntityType 'EventBands' 没有定义键。定义此 EntityType 的键。
System.Data.Edm.EdmEntitySet:EntityType:EntitySet EventBands 基于没有定义键的 EventBands 类型。
错误消息的含义很明显。但是分辨率不是那么明显。我想我必须重写模型绑定方法,但我不完全确定如何用这种方法映射键。
任何帮助将不胜感激,我在下面列出了有问题的课程。
提前致谢,
乔恩
事件:
#region Properties
private int eventId;
public int EventId
{
get
{
return eventId;
}
set
{
eventId = value;
}
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
private string description;
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
private DateTime startDatetime;
public DateTime StartDateTime
{
get
{
return startDatetime;
}
set
{
startDatetime = value;
}
}
private DateTime endDatetime;
public DateTime EndDateTime
{
get
{
return endDatetime;
}
set
{
endDatetime = value;
}
}
private int venueUserId;
public int VenueUserId
{
get { return venueUserId; }
set { venueUserId = value; }
}
public virtual Venue Venue
{
get;
set;
}
public virtual ICollection<EventReview> Reviews
{
get;
set;
}
public virtual ICollection<EventBands> EventBands
{
get;
set;
}
public virtual ICollection<Fan> Attendees
{
get;
set;
}
#endregion
#region Constructor
public Event()
{
EventBands = new HashSet<EventBands>();
Attendees = new HashSet<Fan>();
StartDateTime = DateTime.Now;
EndDateTime = DateTime.Now.AddDays(14);
}
#endregion
乐队:
public class Band : PostableUser
{
#region Properties
private int genreGenreId;
public int GenreGenreId
{
get { return genreGenreId; }
set { genreGenreId = value; }
}
public virtual Genre Genre
{
get;
set;
}
public virtual ICollection<Album> Albums
{
get;
set;
}
public virtual ICollection<BandReview> Reviews
{
get;
set;
}
public virtual ICollection<EventBands> EventBands
{
get;
set;
}
#endregion
#region Constructor
public Band()
{
EventBands = new HashSet<EventBands>();
}
#endregion
}
事件乐队
#region Properties
private int eventEventId;
public int EventEventId
{
get { return eventEventId; }
set { eventEventId = value; }
}
public virtual Event Event
{
get;
set;
}
private int bandUserId;
public int BandUserId
{
get { return bandUserId; }
set { bandUserId = value; }
}
public virtual Band Band
{
get;
set;
}
private DateTime startDateTime;
public DateTime StartDateTime
{
get { return startDateTime; }
set { startDateTime = value; }
}
private DateTime endDateTime;
public DateTime EndDateTime
{
get { return endDateTime; }
set { endDateTime = value; }
}
#endregion
BandUserId 继承自 User 基类。