[英]EntityFramework 6 relationship mapping to many items
I feel like my mapping should be setup correctly, but when I retrieve an item from the db context, it comes with none of the "mapped" items related to it. 我觉得应该正确设置我的映射,但是当我从数据库上下文中检索一个项目时,它没有任何与之相关的“映射”项目。
The mapping I'm going for is this: 我要的映射是这样的:
Top level item is an "Event". 顶级项目是“事件”。
An event has many "TimeSlot" associated with it. 一个事件具有与之关联的许多“ TimeSlot”。
A TimeSlot has many strings associated with it. 时隙有许多与之关联的字符串。
public class MyEvent
{
private ICollection<TimeSlot> mTimeSlots;
public virtual ICollection<TimeSlot> TimeSlots
{
get{return mTimeSlots ?? (mTimeSlots = new Collection<TimeSlot>());}
set{mTimeSlots = value;}
}
[Key]
public int Id {get;set;}
}
And my TimeSlot looks like: 我的时间槽看起来像:
public class TimeSlot
{
private ICollection<string> mItems;
public virtual ICollection<string> Items
{
get { return mItems ?? (mItems = new Collection<string>()); }
set { mItems = value; }
}
[Key]
public virtual int Id { get; set; }
public virtual string Label { get; set; }
}
My DbContext looks like: 我的DbContext看起来像:
public class MyEventsDataContext : DbContext
{
public MyEventsDataContext()
: base("MyEventsDatabase")
{
// Do nothing.
}
public DbSet<MyEvent> MyEvents { get; set; }
public DbSet<TimeSlot> TimeSlots { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEvent>()
.HasMany<TimeSlot>(e => e.TimeSlots);
}
}
I'm migrating some default data for testing purposes like this: 我正在迁移一些默认数据以进行测试,如下所示:
protected override void Seed(MyEventsDataContext context)
{
// This method will be called after migrating to the latest version.
var timeSlot1 = new TimeSlot {Label = "Slot 1"};
timeSlot1.Items.Add("Do stuff 1");
timeSlot1.Items.Add("Do stuff 2");
timeSlot1.Items.Add("Do stuff 3");
var timeSlot2 = new TimeSlot {Label = "Slot 2"};
timeSlot2.Items.Add("Do stuff 1");
timeSlot2.Items.Add("Do stuff 2");
timeSlot2.Items.Add("Do stuff 3");
var timeSlot3 = new TimeSlot {Label = "Slot 3"};
timeSlot3.Items.Add("Do stuff 1");
timeSlot3.Items.Add("Do stuff 2");
timeSlot3.Items.Add("Do stuff 3");
context.TimeSlots.AddOrUpdate(new[] {timeSlot1, timeSlot2, timeSlot3});
var event1 = 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> {timeSlot1, timeSlot2, timeSlot3}
};
context.MyEvents.AddOrUpdate(event1);
var event2 = new MyEvent
{
Address = "321 Street Ln",
CampaignId = "123abc",
City = "City",
CreatedDate = DateTime.Now,
EventDate = DateTime.Now,
EventType = "Conference",
Name = "Show Name",
ProductInterest = "MyArm",
State = "State",
Zipcode = "54321",
TimeSlots = new Collection<TimeSlot> {timeSlot1, timeSlot2, timeSlot3}
};
context.MyEvents.AddOrUpdate(event2);
}
And in my Controller, when I go to retrieve an event from the DbContext it has no attached TimeSlots to it (it's empty). 在我的控制器中,当我从DbContext检索事件时,它没有附加的时隙(它为空)。
MyEvent show = mDb.MyEvents.Find(id);
Any advice here would be much appreciated. 这里的任何建议将不胜感激。 I am probably just not setting up the mapping correctly. 我可能只是没有正确设置映射。
You have to turn on the LazyLoading 您必须打开LazyLoading
public MyEventsDataContext()
: base("MyEventsDatabase")
{
this.Configuration.LazyLoadingEnabled = true;
}
or you should include the property. 或者您应该包括该属性。
mDb.MyEvents.Include("TimeSlots").Find(id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.