Adding a collection, loop needed?
I have this code:
Schedule s = _entities.Schedules.Where(x => x.ScheduleID == schedule.ScheduleID && x.BookingObject.BookingObjectID == bookingObjectID).FirstOrDefault();
if(s == null)
s = new Schedule();
s.ScheduleStart = schedule.ScheduleStart;
s.ScheduleEnd = schedule.ScheduleEnd;
foreach (var t in schedule.Timetables)
{
s.Timetables.Add(t);
}
_entities.AddToSchedules(s);
_entities.SaveChanges();
Exception: System.InvalidOperationException
the object schedule gets passed in as a parameter
EDIT:
changed to:
Schedule sh = new Schedule();
sh.ScheduleStart = schedule.ScheduleStart;
sh.ScheduleEnd = schedule.ScheduleEnd;
foreach (var t in schedule.Timetables)
{
//sh.Timetables.Add(t); // doesn't work
sh.Timetables.Add(new Timetable { DayOfWeek = t.DayOfWeek, StartTime = t.StartTime, EndTime = t.EndTime }); // works
}
_entities.AddToSchedules(sh);
_entities.SaveChanges();
is there some logical reaso开发者_运维百科n why sh.Timetables.Add(t) doesn't work since its a collection of Timetable?
/M
foreach(var time in schedule.Timetables){
s.Timetables.Add(time)
}
You can loop over the time tables in schedule.Timetables and add them to your new entity.
UPDATE: Just confirmed that this is likely to be the reason. And created a new tip in my tips series to explain.
I can think of one possible reason.Is the relationship between schedule.Timetables
one to many? I.e. Can a timetable only be assigned to one schedule at a time?
If so, this is failing:
foreach (var t in schedule.Timetables) {
sh.Timetables.Add(t);
}
Because as you add the 't' to a different schedule, it is automatically removed from the old schedule.Timetables
collection, which of course modifies the collection you are enumerating, which is a no-no.
If so, the solution is simply to do something like this:
var timetables = schedule.Timetables.ToArray();
foreach (var t in timetables) {
sh.Timetables.Add(t);
}
Here you enumerate the collection, before you start modifying it.
s.Timetables.Add(schedule.Timetables)
if this isn't the case, it's likely your question needs more information. What is s.Timetables, schedule.Timetables? (types)
Another option would be:
((List<TimeTable>)s.Timetables).AddRange(((List<TimeTable>)schedule.Timetables));
Look at the first few lines:
Schedule s = _entities.Schedules.Where(x => x.ScheduleID == schedule.ScheduleID && x.BookingObject.BookingObjectID == bookingObjectID).FirstOrDefault();
if(s == null)
s = new Schedule();
On the first line, you're querying the database for an object with the same ScheduleID as schedule
and storing the returned reference in s
. Assuming that object is available in the database, you're effectively pointing s
at the same object as schedule
. That means s.Timetables
and schedule.Timetables
are the same collection, which will cause the exception you're getting during the foreach.
I assume you only want to clone the object pointed to by schedule and insert it into the database if it is not already in the database. In that case:
Schedule s = _entities.Schedules.Where(x => x.ScheduleID == schedule.ScheduleID && x.BookingObject.BookingObjectID == bookingObjectID).FirstOrDefault();
if(s == null)
{
s = new Schedule();
s.ScheduleStart = schedule.ScheduleStart;
s.ScheduleEnd = schedule.ScheduleEnd;
foreach (var t in schedule.Timetables)
{
s.Timetables.Add(t);
}
_entities.AddToSchedules(s);
_entities.SaveChanges();
}
精彩评论