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:
public
IList Resources
{
get
{
if
(
this
.resources ==
null
)
{
this
.resources =
new
List<
object
>();
foreach
(var item
in
this
.SqlAppointmentExternalResources.ToList())
this
.resources.Add(item.ExternalResource);
foreach
(var item
in
this
.SqlAppointmentResources.Select(ar => ar.SqlResource).ToList())
this
.resources.Add((IResource)item);
}
return
this
.resources;
}
}
void
IEditableObject.EndEdit()
{
#region Standard Telerik Appointment Resources
var temp =
this
.SqlAppointmentResources.ToList();
var resources =
this
.Resources.OfType<SqlResource>().ToList();
foreach
(var item
in
temp)
ScheduleViewRepository.Context.SqlAppointmentResources.DeleteObject(item);
foreach
(var sqlResource
in
resources)
ScheduleViewRepository.Context.AddToSqlAppointmentResources(
new
SqlAppointmentResource { SqlAppointment =
this
, SqlResources_SqlResourceId = sqlResource.SqlResourceId });
#endregion
#region External Appointment Resources
var temp1 =
this
.SqlAppointmentExternalResources.ToList();
var externalResources =
this
.Resources.OfType<SqlExternalResource>().ToList();
foreach
(var item
in
temp1)
{
ScheduleViewRepository.Context.SqlAppointmentExternalResources.DeleteObject(item);
}
foreach
(var sqlResource
in
externalResources)
{
ScheduleViewRepository.Context.AddToSqlAppointmentExternalResources(
new
SqlAppointmentExternalResource { SqlAppointment =
this
, SqlResourceTypes_SqlResourceTypeId = sqlResource.ResourceTypeId, ResourceName = sqlResource.ResourceName });
}
#endregion
var removedExceptionAppointments =
this
.exceptionAppointments.Except(
this
.SqlExceptionOccurrences.Select(o => o.Appointment).OfType<SqlExceptionAppointment>());
foreach
(var exceptionAppointment
in
removedExceptionAppointments)
{
var excResources = exceptionAppointment.SqlExceptionResources.ToList();
foreach
(var item
in
excResources)
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!