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.
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:
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).
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
public MyEventsDataContext()
: base("MyEventsDatabase")
{
this.Configuration.LazyLoadingEnabled = true;
}
or you should include the property.
mDb.MyEvents.Include("TimeSlots").Find(id)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.