public static ControllerElementMappingCollectionModel AsModel(this IControllerElementMappingProfile @this, string profileName) { return(new ControllerElementMappingCollectionModel { ProfileID = @this.ProfileGuid, ProfileName = profileName, DriverType = @this.DriverType, ControllerID = @this.ControllerID, DeviceName = @this.DeviceName, VendorID = @this.VendorID, MappedElements = @this.Select(e => e.AsModel(@this.ProfileGuid)).ToList() }); }
public void UpdateMappings(IControllerElementMappingProfile mappings) { using var context = new DatabaseContext(this.Options.Options); var retrievedMappings = context.ControllerElementMappings .Include(p => p.MappedElements) .SingleOrDefault(p => p.ProfileID == mappings.ProfileGuid); if (retrievedMappings == null) { return; } var mappingSet = mappings.Select(k => k.LayoutElement).ToHashSet(); foreach (var mapping in retrievedMappings.MappedElements) { try { if (mappings[mapping.LayoutElement] == mapping.DeviceCapability) { continue; } mapping.DeviceCapability = mappings[mapping.LayoutElement]; context.Entry(mapping).State = EntityState.Modified; } finally { mappingSet.Remove(mapping.LayoutElement); } } foreach (var toChange in mappingSet) { var model = new ControllerElementMappingModel { LayoutElement = toChange, DeviceCapability = mappings[toChange], Collection = retrievedMappings, ProfileID = retrievedMappings.ProfileID }; retrievedMappings.MappedElements.Add(model); context.Entry(model).State = EntityState.Added; } context.SaveChanges(); }