/// <summary> /// Evaluates a collection of entities, and takes care of the whole CRUD operation. /// Meaning, it checks existance in database, and if not found, its created in database. /// If it exists in database with different info, it updates the database. /// In other words, "syncs" the received collection of entities to the database. /// /// </summary> /// <param name="Entities">A list of Entities to be added / updated to the database ///</param> public void PushToDbset(List <InstanceContent> Entities) { //Extract Contained Classes, and push them to the database List <Attribute> Attributes_ToPush = Entities.Select(c => c.Attribute).ToList(); List <Instance> Instances_ToPush = Entities.Select(c => c.Instance).ToList(); List <IOTag> IOTags_ToPush = Entities.Select(c => c.IOTag).ToList(); List <PLCTag> PLCTags_ToPush = Entities.Select(c => c.PLCTag).ToList(); Attributes.PushToDbset(Attributes_ToPush); Instances.PushToDbset(Instances_ToPush); IOTags.PushToDbset(IOTags_ToPush); PLCTags.PushToDbset(PLCTags_ToPush); context.SaveChanges(); //Update the contained classes in the entities, so that they are not null or incomplete List <InstanceContent> EntsToPush = GetListSyncFromDB(Entities); //Find contents that as NOT in the database, and insert them List <InstanceContent> Entities_NOTinDb = NOTInDatabase(EntsToPush); if (Entities_NOTinDb.Count > 0) { InsertList(Entities_NOTinDb); } //Find contents that are in the database, and update them List <InstanceContent> Entities_inDb = InDatabase(EntsToPush); if (Entities_inDb.Count > 0) { UpdateList(Entities_inDb); } }
public List <InstanceContent> GetListSyncFromDB(List <InstanceContent> _Entities) { List <InstanceContent> UpdatedList = (from Cont in _Entities select new InstanceContent() { InstanceContentID = GetID(Cont.Instance.Name, Cont.Attribute.Name), Instance = Instances.GetSyncFromDB(Cont.Instance), InstanceID = Instances.GetID(Cont.Instance.Name), Attribute = Attributes.GetSyncFromDB(Cont.Attribute), AttributeID = Attributes.GetID(Cont.Attribute.Name), PLCTag = Cont.PLCTag != null ? PLCTags.GetSyncFromDB(Cont.PLCTag) : null, PLCTagID = Cont.PLCTag != null ? PLCTags.GetID(Cont.PLCTag.Name) : Cont.PLCTagID, IOTag = IOTags.GetSyncFromDB(Cont.IOTag), IOTagID = IOTags.GetID(Cont.IOTag.Name), AssetName = Cont.AssetName }).ToList(); return(UpdatedList); }