0
我試圖保存與該事件相對應的Event和EventDetails。
它僅從循環中保存事件和第一個EventDetail。EF只保存第一個子實體
public class Event
{
[Key]
public int CourseId { get; set; }
..
public virtual ICollection<EventDetail> EventDetails{ get; set; }
}
控制器:
[HttpPost]
public ActionResult Create(Event ev)
{
if (ModelState.IsValid)
{
IQueryable<EventDetail> eventList = ..;
FutureEvents fe = new FutureEvents();
ICollection<FutureEvents> feCol = new Collection<FutureEvents>();
foreach (EventDetail det in eventList)
{
fe.Name = ..;
db.Entry(fe).State = EntityState.Added;
feCol.Add(fe);
}
ev.EventDetails = feCol;
db.Event.Add(ev);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(ev);
}
以及我不能相信我做到了! – user1770609