1
许多关系,我有以下情况:实体框架代码优先许多与现有实体
modelBuilder.Entity<RecurringAppointment>()
.HasMany(ra => ra.StaffMembers)
.WithMany();
所以工作人员(从用户继承)可以有许多定期apointments,反之亦然。现在,当我尝试的工作人员加入到这样recurringAppointment.StaffMembers ...
[Invoke]
public void ApplyStaffMembersToRecurringAppointment(int recurringAppointmentId, int[] staffMemberIds)
{
var staffs = DbContext.Users
.OfType<Staff>()
.Where(s => staffMemberIds.Contains(s.Id))
.ToList();
var recurringAppointment = DbContext.RecurringAppointments
.Include("StaffMembers")
.Single(ra => ra.Id == recurringAppointmentId);
foreach (var staff in staffs)
{
recurringAppointment.StaffMembers.Add(staff);
DbContext.Users.Attach(staff);
}
DbContext.RecurringAppointments.Attach(recurringAppointment);
DbContext.Entry(recurringAppointment).State = EntityState.Modified;
DbContext.SaveChanges();
}
...它只是不会保存更改。当我手动将值添加到数据库时,它完美地工作,所以关系应该正确设置。
我看着万吨类似的条目,但他们都既没有保存或不重视。代码被执行(我可以调试)。可能是什么问题呢?
感谢!!!!!!!!! – LueTm 2012-08-06 13:52:16