EntityFramework删除在更新之前发生时也插入

时间:2016-06-06 09:30:31

标签: c# sql .net entity-framework entity-framework-6

我在单个SaveChanges中尝试(可能做得太多)时收到外键错误。

基本上我有以下3个poco实体。

    public class ClinicianAvailability
    {
        public int ClinicianAvailabilityId { get; set; }
    }

 public class SurgicalBooking
    {
        public int SurgicalBookingId
        public int? ClinicianAvailabilityId { get; set; }
        public bool IsAdhoc { get; set; }

        public virtual TheatreBooking TheatreBooking { get; set; }
        public virtual ClinicianAvailability ClinicianAvailability { get; set; }
    }

    public class TheatreBooking
    {
        public int TheatreBookingId {get;set;}

        public virtual SurgicalBooking SurgicalBooking { get; set; }
        public virtual ClinicianAvailability ClinicianAvailability { get; private set; }
    }

我基本上试图删除一个ClinicianAvailability,它在SurgicalBooking上有一个外键。但同时试图在SurgicalBooking上设置一个新的TheatreBooking。

像这样:

var entity = _clinicianAvailabilityRepository.Find(resourceAvailabilityId);
    var surgStub = surgicalBookingRepository.CreateStub(bookingData.ScheduleId);
    surgStub.IsAdhoc = true;
    surgStub.ClinicianAvailabilityId = null;
surgicalBookingRepository.SetModified(surgStub, new Expression<Func<SurgicalBooking, object>>[] { x => x.IsAdhoc, x => x.ClinicianAvailabilityId });

    theatreBookingRepository.Add( new TheatreBooking
    {
                                    TheatreBooking Id = theatreBookingRepository.GetNewTempKey(),
                                    TheatreId = associatedTheatreId.Value,                           
                                    SurgicalBooking = surgStub
                                });
                                theatreBookingRepository.Add(TheatreBooking);
_clinicianAvailabilityRepository.Remove(entity);
_clinicianAvailabilityRepository.UnitOfWork.SaveChanges();

所以基本上这样做后,我在ClinicianAvailabilityId的SurgicalBooking外键上出现了外键错误。如果我取出添加TheatreBooking,它会按照正确的顺序更新Surgical Booking然后删除。但是通过添加剧院预订,它首先尝试删除然后失败。对此有何帮助?我试图尽可能地简化这个,但它有点复杂。我正在尝试不进行多次保存更改或将其全部放在一个事务中,因为更改所有这些代码会进行大量的返工。

我查看了ChangeTracker,所有项目似乎都按照正确的顺序排列,但它没有按顺序执行。

1 个答案:

答案 0 :(得分:0)

我尝试使用以下代码复制错误,但它已成功完成:

    static void CreateAndSeedDatabase()
    {
        Context context = new Context();
        ClinicianAvailability cAvail = new ClinicianAvailability() { };
        SurgicalBooking sBooking = new SurgicalBooking() { IsAdhoc = true, ClinicianAvailability = cAvail };
        context.SurgicalBookings.Add(sBooking);
        context.SaveChanges();
    }

    static void DeleteUpdateInsert()
    {
        Context context = new Context();
        ClinicianAvailability cAvail = context.ClinicianAvailabilitys.Find(1);
        SurgicalBooking sBooking = context.SurgicalBookings.Find(1);
        sBooking.IsAdhoc = false;
        sBooking.ClinicianAvailability = null;
        TheatreBooking tBooking = new TheatreBooking(){SurgicalBooking = sBooking};
        context.TheatreBookings.Add(tBooking);
        context.ClinicianAvailabilitys.Remove(cAvail);
        context.SaveChanges();
    }

上下文类:

public class Context : DbContext
{
    public Context()
    {
        Database.SetInitializer<Context>(new CreateDatabaseIfNotExists<Context>());
        Database.Initialize(true);
    }

    public DbSet<ClinicianAvailability> ClinicianAvailabilitys { get; set; }
    public DbSet<SurgicalBooking> SurgicalBookings { get; set; }
    public DbSet<TheatreBooking> TheatreBookings { get; set; }
}

修改过的POCO课程:

public class ClinicianAvailability
{
    public int ClinicianAvailabilityId { get; set; }
}

public class SurgicalBooking
{
    public int SurgicalBookingId { get; set; }
    public bool IsAdhoc { get; set; }
    public virtual ClinicianAvailability ClinicianAvailability { get; set; }
}

public class TheatreBooking
{
    public int TheatreBookingId { get; set; }

    public virtual SurgicalBooking SurgicalBooking { get; set; }
    public virtual ClinicianAvailability ClinicianAvailability { get; private set; }
}