2014-12-01 183 views
1

我一直在使用EF一段時間,但只使用基本的規範化表,以及它們之間的小關係。現在我正在尋找分支並遇到一些映射問題,我確信這些問題只是通過一些簡單的OnModelCreating()更改或其他模型屬性解決。實體框架6映射問題

我正在創建一個網站來管理可能發生在各個位置的事件。每個事件都是一個實體。每個事件都有一些基本的基本屬性,但它也有一個虛擬的ICollection<TimeSlot>

public virtual ICollection<TimeSlot> TimeSlots 
    { 
     get { return mTimeSlots ?? (mTimeSlots = new Collection<TimeSlot>()); } 
     set { mTimeSlots = value; } 
    } 

TimeSlot非常簡單過,它應該代表一個容器的活動的集合在特定時間發生。

public class TimeSlot 
{ 
    private ICollection<TimeSlotItem> mItems; 

    public virtual ICollection<TimeSlotItem> Items 
    { 
     get { return mItems ?? (mItems = new Collection<TimeSlotItem>()); } 
     set { mItems = value; } 
    } 

    [Key] 
    public virtual int Id { get; set; } 

    public virtual string Label { get; set; } 
} 

由於EF不能映射到基元類型(在這種情況下字符串)的集合,我創建稱爲TimeSlotItem另一個實體,這簡直是一個字符串實體映射。

public class TimeSlotItem 
{ 
    [Key] 
    public virtual int Id { get; set; } 

    public virtual string Description { get; set; } 
} 

我的問題是如何將這一切映射在一起。 EF默認情況下不會正確映射這些數據,因爲當我爲數據庫播種一些事件,時隙和時間條目時,它只是簡單地將所有事件映射到其中一個事件(第一個事件),而沒有其他映射。我不認爲外鍵設置正確映射。目前這可能不是多對多,但至少我相信它應該是。

我的映射是:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<FaroEvent>() 
      .HasMany(f => f.TimeSlots); 
     modelBuilder.Entity<TimeSlot>() 
      .HasMany(f => f.Items); 
    } 

延遲加載在構造函數啓用。

我的種子是:

protected override void Seed(MyEventsDataContext context) 
    { 
     // This method will be called after migrating to the latest version. 

     var timeSlotItems = new List<TimeSlotItem> 
     { 
      new TimeSlotItem {Description = "Do stuff 1"}, 
      new TimeSlotItem {Description = "Do stuff 2"}, 
      new TimeSlotItem {Description = "Do stuff 3"}, 
     }; 
     timeSlotItems.ForEach(t => context.TimeSlotItems.AddOrUpdate(i => i.Description, t)); 
     context.SaveChanges(); 

     var timeSlots = new List<TimeSlot> 
     { 
      new TimeSlot 
      { 
       Label = "Slot 1", 
       Items = new Collection<TimeSlotItem> {timeSlotItems[0], timeSlotItems[1], timeSlotItems[2]} 
      }, 
      new TimeSlot 
      { 
       Label = "Slot 2", 
       Items = new Collection<TimeSlotItem> {timeSlotItems[0], timeSlotItems[1], timeSlotItems[2]} 
      }, 
      new TimeSlot 
      { 
       Label = "Slot 3", 
       Items = new Collection<TimeSlotItem> {timeSlotItems[0], timeSlotItems[1], timeSlotItems[2]} 
      }, 
     }; 
     timeSlots.ForEach(t => context.TimeSlots.AddOrUpdate(i => i.Label, t)); 
     context.SaveChanges(); 

     var events = new List<MyEvent> 
     { 
      new MyEvent 
      { 
       Address = "123 Street Ln", 
       CampaignId = "abc123", 
       City = "City", 
       CreatedDate = DateTime.Now, 
       EventDate = DateTime.Now, 
       EventType = "TradeShow", 
       Name = "Show Name", 
       ProductInterest = "MyArm", 
       State = "State", 
       Zipcode = "12345", 
       TimeSlots = new Collection<TimeSlot> {timeSlots[0], timeSlots[1], timeSlots[2]} 
      }, 
      new MyEvent 
      { 
       Address = "123 Street Ln", 
       CampaignId = "abc123", 
       City = "City", 
       CreatedDate = DateTime.Now, 
       EventDate = DateTime.Now, 
       EventType = "TradeShow", 
       Name = "Show Name", 
       ProductInterest = "MyArm", 
       State = "State", 
       Zipcode = "12345", 
       TimeSlots = new Collection<TimeSlot> {timeSlots[0], timeSlots[1], timeSlots[2]} 
      }, 
      new MyEvent 
      { 
       Address = "123 Street Ln", 
       CampaignId = "abc123", 
       City = "City", 
       CreatedDate = DateTime.Now, 
       EventDate = DateTime.Now, 
       EventType = "TradeShow", 
       Name = "Show Name", 
       ProductInterest = "MyArm", 
       State = "State", 
       Zipcode = "12345", 
       TimeSlots = new Collection<TimeSlot> {timeSlots[0], timeSlots[1], timeSlots[2]} 
      }, 
     }; 
     events.ForEach(t => context.MyEvents.AddOrUpdate(i => i.Name, t)); 
     context.SaveChanges(); 
    } 
+1

種子方法似乎期望多對多的關聯,但是您的模型具有一對多關係。它應該是什麼? – 2014-12-01 22:30:52

+0

是的,我認爲這是問題所在。我猜想創建多對多我的模型應該引用回其他實體的列表。 – mariocatch 2014-12-02 15:18:38

+0

是的,Gert,就是這樣。如果您將此作爲答案,我可以將其標記爲正確(只需要每個模型實際表示其自身的多對多關係)。 – mariocatch 2014-12-02 15:38:47

回答