This is a migrated thread and some comments may be shown as answers.

Error when deleting an appointment

4 Answers 93 Views
ScheduleView
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 30 Aug 2011, 08:47 PM
I am receiving the following error when attempting to call ScheduleViewRepository.Context.SubmitChanges() when I have removed an appointment from the ScheduleView:
"Submit operation failed. An error occurred while updating the entries. See the inner exception for details. InnerException message: The DELETE statement conflicted with the REFERENCE constraint \"FK_SqlAppointmentResources_SqlAppointment\". The conflict occurred in database \"ScheduleView\", table \"dbo.SqlAppointmentResources\", column 'SqlAppointments_SqlAppointmentId'.\r\nThe statement has been terminated."
Am I missing a method call?
Here is the code that is being called when saving the schedule:

 

private void OnSaveCommandExecute(object param)

{

 

if (ScheduleViewRepository.Context.HasChanges)

{

 

try

{

 

ScheduleViewRepository.Context.SubmitChanges().Completed += (s, a) =>

{

 

while (ScheduleViewRepository.DeletedAppointments.Count > 0)

{

 

ScheduleViewRepository.Context.SqlAppointments.Remove(ScheduleViewRepository.DeletedAppointments.Dequeue());

}

 

ScheduleViewRepository.Context.SubmitChanges(); // This is the line that fails
};

 

}

 

catch (System.Exception)

{

 

throw;

}

}

}


4 Answers, 1 is accepted

Sort by
0
Rosi
Telerik team
answered on 02 Sep 2011, 03:37 PM
Hello Eric,

We are not sure what causes the exception. However you can review the following forum post:
http://www.telerik.com/community/forums/silverlight/scheduleview/database-table-structure.aspx

It illustrates how you can complete the task and contains attached a project that works as expected.


Best wishes,
Rosi
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Eric
Top achievements
Rank 1
answered on 06 Sep 2011, 05:13 PM
Thank you for your response, however, the sample project in the thread (ScheduleView_EF_SL) is what I'm using as a code base. I will get a fresh copy of the project and compare my differences to see if I can find where the issue lies. It is probably in my entity model since I have made some extensive modifications to it... Knowing yours works as expected does narrow it down for me though.

Thanks for the help.
0
Eric
Top achievements
Rank 1
answered on 12 Sep 2011, 03:37 PM
I ended up adding an Active flag to the entity for SqlAppointments and modified my CollectionChanged method to support logical deletes. This was a requirement by my client, and now I don't run into any more issues. Never figured out where there was a discrepancy in my entity model, but I believe it had something to do with the relationships and not properly cascading the deletes. I'm still unsure as my code was based on the sample project provided in the other thread and those portions (Appointment entity model and CollectionChanged event) were identical from one project to the next. Anyway, here is my one minor change compared to the original in case anyone wants to see how to handle the logical delete after adding the flag to the table:
void AppointmentTemplates_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                var app = e.NewItems == null ? null : e.NewItems[0] as AppointmentTemplate;
                if (app != null && app.EntityState != EntityState.Unmodified)
                {
                    app.Active = true;
                    ScheduleViewRepository.Context.AppointmentTemplates.Add(app);
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                var app = e.OldItems == null ? null : e.OldItems[0] as AppointmentTemplate;
                if (app != null && ScheduleViewRepository.Context.AppointmentTemplates.Contains(app))
                {
                    //ScheduleViewRepository.Context.AppointmentTemplates.Remove(app);
                    app.Active = false;
                }
            }
        }
0
Eric
Top achievements
Rank 1
answered on 12 Sep 2011, 03:39 PM
Oh, I almost forgot that I had to change the OnSaveCommandExecute method as well. Simple one line change shown below:
private void OnSaveCommandExecute(object param)
        {
            if (ScheduleViewRepository.Context.HasChanges)
            {
                try
                {
                    ScheduleViewRepository.Context.SubmitChanges().Completed += (s, a) =>
                    {
                        while (ScheduleViewRepository.DeletedAppointments.Count > 0)
                        {
                            //ScheduleViewRepository.Context.SqlAppointments.Remove(ScheduleViewRepository.DeletedAppointments.Dequeue());
                            ScheduleViewRepository.DeletedAppointments.Dequeue();
                        }
 
                        ScheduleViewRepository.Context.SubmitChanges();
                    };
                }
                catch (System.Exception)
                {
                    throw;
                }
            }
        }
Tags
ScheduleView
Asked by
Eric
Top achievements
Rank 1
Answers by
Rosi
Telerik team
Eric
Top achievements
Rank 1
Share this question
or