public void SaveSubGroup(SubGroup group) { if (group.SubGroupId != -1) { SubGroup.Add(group); } else { var temp = SubGroup.FirstOrDefault(s => s.SubGroupId == group.SubGroupId); if (temp != null) { temp = group; } else { SubGroup.Add(group); } } SaveChanges(); }