Error when deleting an appointment

5 posts, 0 answers
  1. Eric
    Eric avatar
    7 posts
    Member since:
    Aug 2011

    Posted 30 Aug 2011 Link to this post

    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;

    }

    }

    }


  2. Rosi
    Admin
    Rosi avatar
    2529 posts

    Posted 02 Sep 2011 Link to this post

    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 >>

  3. Eric
    Eric avatar
    7 posts
    Member since:
    Aug 2011

    Posted 06 Sep 2011 Link to this post

    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.
  4. Eric
    Eric avatar
    7 posts
    Member since:
    Aug 2011

    Posted 12 Sep 2011 Link to this post

    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;
                    }
                }
            }
  5. Eric
    Eric avatar
    7 posts
    Member since:
    Aug 2011

    Posted 12 Sep 2011 Link to this post

    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;
                    }
                }
            }
Back to Top