我使用EF已经有一段时间了,但只使用基本的规范化表格,它们之间几乎没有关系。现在,我希望扩展到一些映射问题,我相信这些问题可以通过一些简单的OnModelCreating()更改或其他模型属性来解决。
我正在创建一个网站来管理可能在不同地点发生的事件。每个事件都是一个实体。每个事件都有一些基本的原语属性,但它也有一个虚拟的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不能映射到原语类型(本例中为string )的集合,因此我创建了另一个名为TimeSlotItem的实体,这只是一个string实体映射。
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);
}在ctor中启用了延迟加载。
我的种子是:
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();
}发布于 2014-12-02 05:59:56
试试这个:
modelBuilder.Entity<FaroEvent>().HasMany(f => f.TimeSlots).WithMany(f => f.Items);你可以在这里找到更好的解释:http://msdn.microsoft.com/en-us/data/jj591620.aspx#ManyToMany
发布于 2014-12-02 11:28:26
为了更好地理解Fluent并理解如何映射实体,我过去常常使用Entity Framework Power Tools或Entity Framework Reverse Code-First POCO Generator工具,在我定义了我的表在SQL数据库中应该是什么样子之后,让该工具为我生成类。这种方法可以帮助您开始使用Fluent API并更好地理解它。
希望这能有所帮助。
https://stackoverflow.com/questions/27237999
复制相似问题