I've started modifying the Database SDK example to support resources that are not in MSSQL so that we can continue to use our external resources that are in different database formats until we can migrate our entire application.
I've gotten it to load and save the data appropriately to the new AppointmentExternalResources link table, however when I edit an appointment and click save it thinks there are duplicate resources in the this.Resources of the SqlAppointment and then gives an error when saving the ScheduleViewRepository.Context.
I've made the following changes:
- SqlResourceTypes - Added ExternalResource (bit)
- New table SqlAppointmentExternalResources
- SqlAppointments_SqlAppointmentId (int)
- SqlResourceTypes_SqlResourceTypeId (int)
- ResourceName (nvarchar(100)) - Used for the key value of the external database
Code wise:
public partial class SqlAppointmentExternalResource { #region Constants and Variables short bStat = 0; SqlExternalResource externalResource; #endregion #region Properties public SqlExternalResource ExternalResource { get { if (externalResource == null && !string.IsNullOrEmpty(this.ResourceName)) { externalResource = new SqlExternalResource(); externalResource.ResourceTypeId = this.SqlResourceTypes_SqlResourceTypeId; switch (this.SqlResourceTypes_SqlResourceTypeId) { case 2://load from external database externalResource.ResourceName = "XXX"; externalResource.DisplayName = "DISPLAY_NAME"; break; case 4://load from external database externalResource.ResourceName = "ADMIN"; externalResource.DisplayName = "ADMINISTRATION"; break; } } return externalResource; } set { externalResource = value; this.OnPropertyChanged("ExternalResource"); } } #endregion }- SqlAppointment was changed as follows:
publicIList Resources{get{if(this.resources ==null){this.resources =newList<object>();foreach(var iteminthis.SqlAppointmentExternalResources.ToList())this.resources.Add(item.ExternalResource);foreach(var iteminthis.SqlAppointmentResources.Select(ar => ar.SqlResource).ToList())this.resources.Add((IResource)item);}returnthis.resources;}}voidIEditableObject.EndEdit(){#region Standard Telerik Appointment Resourcesvar temp =this.SqlAppointmentResources.ToList();var resources =this.Resources.OfType<SqlResource>().ToList();foreach(var itemintemp)ScheduleViewRepository.Context.SqlAppointmentResources.DeleteObject(item);foreach(var sqlResourceinresources)ScheduleViewRepository.Context.AddToSqlAppointmentResources(newSqlAppointmentResource { SqlAppointment =this, SqlResources_SqlResourceId = sqlResource.SqlResourceId });#endregion#region External Appointment Resourcesvar temp1 =this.SqlAppointmentExternalResources.ToList();var externalResources =this.Resources.OfType<SqlExternalResource>().ToList();foreach(var itemintemp1){ScheduleViewRepository.Context.SqlAppointmentExternalResources.DeleteObject(item);}foreach(var sqlResourceinexternalResources){ScheduleViewRepository.Context.AddToSqlAppointmentExternalResources(newSqlAppointmentExternalResource { SqlAppointment =this, SqlResourceTypes_SqlResourceTypeId = sqlResource.ResourceTypeId, ResourceName = sqlResource.ResourceName });}#endregionvar removedExceptionAppointments =this.exceptionAppointments.Except(this.SqlExceptionOccurrences.Select(o => o.Appointment).OfType<SqlExceptionAppointment>());foreach(var exceptionAppointmentinremovedExceptionAppointments){var excResources = exceptionAppointment.SqlExceptionResources.ToList();foreach(var iteminexcResources)ScheduleViewRepository.Context.SqlExceptionResources.DeleteObject(item);}ScheduleViewRepository.Context.SaveChanges();} - So in the EndEdit function it has 4 resources in this.Resource and 2 are duplicates of the original 2. I'm not sure where these duplicates are coming from. Any ideas or help would be greatly appreciated!