我有以下情况:
modelBuilder.Entity<RecurringAppointment>()
.HasMany(ra => ra.StaffMembers)
.WithMany();
所以一个工作人员(从用户继承)可以有很多重复的约会,反之亦然。现在,当我尝试像这样将工作人员添加到 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();
}
...它只是不会保存更改。当我手动向数据库添加值时,它可以完美运行,因此应该正确设置关系。
我查看了大量类似的条目,但它们要么没有保存,要么没有附加。代码被执行(我可以调试)。可能是什么问题呢?