저는 한동안 EF를 사용했지만 기본 정규화 된 테이블과 그 사이에 거의 관계가 없습니다. 이제는 몇 가지 매핑 문제를 찾아서 실행하려고합니다. 간단히 말해서 OnModelCreating()
변경 사항이나 추가 모델 속성으로 해결할 수 있습니다.Entity Framework 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);
}
게으른 로딩이의 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();
}
seed 메서드는 many-to-many 연관을 기대하지만, 여러분의 모델은 one-to-many를 가진다. 그것은 무엇이어야 하는가? –
예 그게 문제라고 생각합니다. 나는 many-to-many 모델을 만드는 것으로 추측하고있다. 다른 모델은 다른 엔티티의리스트를 다시 참조해야한다. – mariocatch
예 Gert, 이것이 그랬습니다. 이것을 대답으로 만들면 정확한 것으로 표시 할 수 있습니다 (실제로는 각 모델이 실제로 다 대다 관계를 나타 내기 위해 필요합니다). – mariocatch