public override void PushToDbset(List <IOTag> Entities) { List <PLC> PLCs_ToPush = Entities .OfType <IOTag>() .Select(e => e.PLC) .OfType <PLC>() .GroupBy(vPLC => vPLC.Name) .Select(vPLC => vPLC.First()).ToList(); if (PLCs_ToPush.Any()) { PLCs.PushToDbset(PLCs_ToPush); context.SaveChanges(); } List <IOTag> EntsToPush = GetListSyncFromDB(Entities); List <IOTag> Entities_NOTinDb = NOTInDatabase(EntsToPush); if (Entities_NOTinDb.Count > 0) { InsertList(Entities_NOTinDb); } List <IOTag> Entities_inDb = InDatabase(EntsToPush); if (Entities_inDb.Count > 0) { UpdateList(Entities_inDb); } }
public override IOTag GetSyncFromDB(IOTag _Entity) { IOTag ret = new IOTag(); var FoundEntity = context.Set <IOTag>().FirstOrDefault(e => e.Name == _Entity.Name && e.PLC.Name == _Entity.PLC.Name); if (FoundEntity == null) { ret = new IOTag() { Name = _Entity.Name, PLC = PLCs.GetSyncFromDB(_Entity.PLC), PLCID = PLCs.GetID(_Entity.PLC.Name) }; } else { FoundEntity.PLC = PLCs.GetSyncFromDB(_Entity.PLC); ret = FoundEntity; } return(ret); }
public override PLCTag GetSyncFromDB(PLCTag _Entity) { PLCTag ret = new PLCTag(); var FoundEntity = context.Set <PLCTag>().FirstOrDefault(e => e.Name == _Entity.Name && e.PLC.Name == _Entity.PLC.Name); if (FoundEntity == null) { ret = new PLCTag() { Name = _Entity.Name, PLC = PLCs.GetSyncFromDB(_Entity.PLC), PLCID = PLCs.GetID(_Entity.PLC.Name), Rack = _Entity.Rack, Slot = _Entity.Slot, Point = _Entity.Point }; } else { FoundEntity.PLC = PLCs.GetSyncFromDB(_Entity.PLC); FoundEntity.Rack = _Entity.Rack; FoundEntity.Slot = _Entity.Slot; FoundEntity.Point = _Entity.Point; ret = FoundEntity; } return(ret); }
public async Task <HttpResponseMessage> Post([FromBody] List <InstanceContent> request) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest); response.ReasonPhrase = "Request did not initialize"; Console.WriteLine("#01 - Initializing Program"); List <InstanceContent> vContents = request; //Extract Contained Classes, and push them to the database Console.WriteLine("#02 - Extract Contained Classes, and push them to the database"); List <Model.PLCTag> PLCTags_ToPush = vContents.Select(c => c.PLCTag).ToList(); Contents.PLCTags.PushToDbset(PLCTags_ToPush); Contents.context.SaveChanges(); List <Model.Instance> dbInstances = Contents.Instances.EntityCollection; List <Model.Attribute> dbAttributes = Contents.Attributes.EntityCollection; List <Model.IOTag> dbIOTags = Contents.IOTags.EntityCollection; List <Model.PLCTag> dbPLCTags = Contents.PLCTags.EntityCollection; List <InstanceContent> dbContents = Contents.EntityCollection; //Update the contained classes in the entities, so that they are not null or incomplete //List<InstanceContent> EntsToPush = Contents.GetListSyncFromDB(request); //Contents.context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; Console.WriteLine("#03 - Sync with DB: JOIN TABLES"); List <InstanceContent> EntsToPush = (from Cont in vContents join db_Contents in dbContents on new { InstName = Cont.Instance.Name, AttName = Cont.Attribute.Name } equals new { InstName = db_Contents.Instance.Name, AttName = db_Contents.Attribute.Name } into dbCs from cs in dbCs.DefaultIfEmpty(Cont) join db_Instances in dbInstances on Cont.Instance.Name equals db_Instances.Name into Ins from i in Ins.DefaultIfEmpty(Cont.Instance) join db_Attributes in dbAttributes on Cont.Attribute.Name equals db_Attributes.Name into Atts from a in Atts.DefaultIfEmpty(Cont.Attribute) join db_IOTags in dbIOTags on new { IOTagName = Cont.IOTag.Name, PLCName = Cont.IOTag.PLC.Name } equals new { IOTagName = db_IOTags.Name, PLCName = db_IOTags.PLC.Name } into IOs from io in IOs.DefaultIfEmpty(Cont.IOTag) join db_PLCTags in dbPLCTags on new { PLCTagName = Cont.PLCTag.Name, PLCName = Cont.PLCTag.PLC.Name } equals new { PLCTagName = db_PLCTags.Name, PLCName = db_PLCTags.PLC.Name } into PLCs from P in PLCs.DefaultIfEmpty(Cont.PLCTag) //into IC InstanceContentID = cs != null ? cs.InstanceContentID : -1, select new InstanceContent() { InstanceContentID = cs != null ? cs.InstanceContentID : -1, Instance = i != null ? i : new Model.Instance(), InstanceID = i != null ? i.ID : 0, Attribute = a != null ? a : new Model.Attribute(), AttributeID = a != null ? a.ID : 0, PLCTag = P != null ? P : new Model.PLCTag(), PLCTagID = P != null ? P.ID : 0, IOTag = io != null ? io : new Model.IOTag(), IOTagID = io != null ? io.ID : 0, AssetName = Cont.AssetName }).ToList(); //Find contents that are in the database, and update them //List<InstanceContent> Entities_inDb = Contents.InDatabase(EntsToPush); List <InstanceContent> Entities_inDb = EntsToPush.Where(c => c.InstanceContentID > 0).ToList(); if (Entities_inDb.Count > 0) { Contents.UpdateList(Entities_inDb); } foreach (InstanceContent IC in Entities_inDb) { Console.WriteLine("In DB: ID" + IC.InstanceContentID + ":" + IC.Instance.Name + "." + IC.Attribute.Name + " PLCTagID:" + IC.PLCTag.ID + ":" + IC.PLCTag.Name); } Contents.context.SaveChanges(); response = new HttpResponseMessage(HttpStatusCode.OK); return(response); }