//Link push private static void PushLink(Link link) { using (Context context = new Context()) { var linkEntity = context.Links.Find(link.ID); if (linkEntity == null) { #region Prevent extra messages generation foreach (var message in link.MessageList) { context.Entry <Message>(message).State = EntityState.Unchanged; } #endregion context.Links.Add(link); context.SaveChanges(); } else { context.Entry(linkEntity).Collection(l => l.MessageList).Load(); #region MessageList push foreach (var messageInDB in linkEntity.MessageList) { if (link.MessageList.Find(m => m.ID == messageInDB.ID) == null) { DataInterface <Link, Message> .RemoveRelation(linkEntity.ID, messageInDB.ID); } } foreach (var messageInPush in link.MessageList) { if (linkEntity.MessageList.Find(m => m.ID == messageInPush.ID) == null) { DataInterface <Link, Message> .AddRelation(linkEntity.ID, messageInPush.ID); } } #endregion context.Entry(linkEntity).CurrentValues.SetValues(link); context.SaveChanges(); } } }
//Document push private static void PushDocument(Document document) { using (Context context = new Context()) { var documentEntity = context.Documents.Find(document.ID); if (documentEntity == null) { #region Prevent extra folders generation foreach (var folder in document.FolderList) { context.Entry <Folder>(folder).State = EntityState.Unchanged; } #endregion context.Documents.Add(document); context.SaveChanges(); } else { context.Entry(documentEntity).Collection(d => d.FolderList).Load(); #region FolderList push foreach (var folderInDB in documentEntity.FolderList) { if (document.FolderList.Find(f => f.ID == folderInDB.ID) == null) { DataInterface <Document, Folder> .RemoveRelation(documentEntity.ID, folderInDB.ID); } } foreach (var folderInPush in document.FolderList) { if (documentEntity.FolderList.Find(f => f.ID == folderInPush.ID) == null) { DataInterface <Document, Folder> .AddRelation(documentEntity.ID, folderInPush.ID); } } #endregion context.Entry(documentEntity).CurrentValues.SetValues(document); context.SaveChanges(); } } }
//User push private static void PushUser(User user) { using (Context context = new Context()) { var userEntity = context.Users.Find(user.ID); if (userEntity == null) { #region Prevent extra folders generation foreach (var folder in user.FolderList) { context.Entry <Folder>(folder).State = EntityState.Unchanged; } #endregion #region Prevent extra messages generation foreach (var message in user.MessageList) { context.Entry <Message>(message).State = EntityState.Unchanged; } #endregion context.Users.Add(user); context.SaveChanges(); } else { context.Entry(userEntity).Collection(u => u.FolderList).Load(); context.Entry(userEntity).Collection(u => u.MessageList).Load(); #region FolderList push foreach (var folderInDB in userEntity.FolderList) { if (user.FolderList.Find(f => f.ID == folderInDB.ID) == null) { DataInterface <User, Folder> .RemoveRelation(userEntity.ID, folderInDB.ID); } } foreach (var folderInPush in user.FolderList) { if (userEntity.FolderList.Find(f => f.ID == folderInPush.ID) == null) { DataInterface <User, Folder> .AddRelation(userEntity.ID, folderInPush.ID); } } #endregion #region MessageList push foreach (var messageInDB in userEntity.MessageList) { if (user.MessageList.Find(f => f.ID == messageInDB.ID) == null) { DataInterface <User, Message> .RemoveRelation(userEntity.ID, messageInDB.ID); } } foreach (var messageInPush in user.MessageList) { if (userEntity.MessageList.Find(f => f.ID == messageInPush.ID) == null) { DataInterface <User, Message> .AddRelation(userEntity.ID, messageInPush.ID); } } #endregion context.Entry(userEntity).CurrentValues.SetValues(user); context.SaveChanges(); } } }
//Message push private static void PushMessage(Message message) { using (Context context = new Context()) { var messageEntity = context.Messages.Find(message.ID); if (messageEntity == null) { #region Prevent extra links generation foreach (var link in message.LinkList) { context.Entry <Link>(link).State = EntityState.Unchanged; } #endregion #region Prevent extra users generation foreach (var user in message.UserList) { context.Entry <User>(user).State = EntityState.Unchanged; } #endregion context.Messages.Add(message); context.SaveChanges(); } else { context.Entry(messageEntity).Collection(m => m.LinkList).Load(); context.Entry(messageEntity).Collection(m => m.UserList).Load(); #region LinkList push foreach (var linkInDB in messageEntity.LinkList) { if (message.LinkList.Find(l => l.ID == linkInDB.ID) == null) { DataInterface <Message, Link> .RemoveRelation(messageEntity.ID, linkInDB.ID); } } foreach (var linkInPush in message.LinkList) { if (messageEntity.LinkList.Find(l => l.ID == linkInPush.ID) == null) { DataInterface <Message, Link> .AddRelation(messageEntity.ID, linkInPush.ID); } } #endregion #region UserList push foreach (var userInDB in messageEntity.UserList) { if (message.UserList.Find(u => u.ID == userInDB.ID) == null) { DataInterface <Message, User> .RemoveRelation(messageEntity.ID, userInDB.ID); } } foreach (var userInPush in message.UserList) { if (messageEntity.UserList.Find(u => u.ID == userInPush.ID) == null) { DataInterface <Message, User> .AddRelation(messageEntity.ID, userInPush.ID); } } #endregion context.Entry(messageEntity).CurrentValues.SetValues(message); context.SaveChanges(); } } }
//Folder push private static void PushFolder(Folder folder) { using (Context context = new Context()) { var folderEntity = context.Folders.Find(folder.ID); if (folderEntity == null) { #region Prevent extra user generation foreach (var user in folder.UserList) { context.Entry <User>(user).State = EntityState.Unchanged; } #endregion #region Prevent extra document generation foreach (var doc in folder.DocumentList) { context.Entry <Document>(doc).State = EntityState.Unchanged; } #endregion context.Folders.Add(folder); context.SaveChanges(); } else { context.Entry(folderEntity).Collection(fol => fol.UserList).Load(); context.Entry(folderEntity).Collection(fol => fol.DocumentList).Load(); #region Userlist push foreach (var userInDB in folderEntity.UserList) { if (folder.UserList.Find(user => user.ID == userInDB.ID) == null) { DataInterface <Folder, User> .RemoveRelation(folderEntity.ID, userInDB.ID); } } foreach (var userInPush in folder.UserList) { if (folderEntity.UserList.Find(user => user.ID == userInPush.ID) == null) { DataInterface <Folder, User> .AddRelation(folderEntity.ID, userInPush.ID); } } #endregion #region DocumentList push foreach (var documentInDB in folderEntity.DocumentList) { if (folder.DocumentList.Find(d => d.ID == documentInDB.ID) == null) { DataInterface <Folder, Document> .RemoveRelation(folderEntity.ID, documentInDB.ID); } } foreach (var documentInPush in folder.DocumentList) { if (folderEntity.DocumentList.Find(d => d.ID == documentInPush.ID) == null) { DataInterface <Folder, Document> .AddRelation(folderEntity.ID, documentInPush.ID); } } #endregion context.Entry(folderEntity).CurrentValues.SetValues(folder); context.SaveChanges(); } } }