public static void UpdateTopic(TopicViewModel topic, ApplicationDbContext context = null) { var ContextInstance = context != null ? context : ApplicationDbContext.Create(); Topic update = GetTopic(topic.Id, ContextInstance); if (topic.ParentId == -1) { update.Level = 0; topic.ParentId = null; } else { Topic parent = GetTopic(topic.ParentId.Value, ContextInstance); update.Level = parent.Level + 1; update.ParentId = topic.ParentId; } update.Name = topic.Name; if (topic.ChildIds != null) { update.Childs = topic.ChildIds.Select(x => ContextInstance.Topics.First(y => y.Id == x)).ToList(); } ContextInstance.SaveChanges(); }