/// <summary> /// 添加Parent及其EasyChatTime /// </summary> /// <param name="contact"></param> /// <param name="studentID"></param> void AddParentAndChattime(EasyChatTimeModel contact, Guid studentID) { StudentParentEntity other = new StudentParentEntity { ParentID = Guid.NewGuid(), StudentID = studentID, NameCn = contact.ContactIdentity.NameCn, Email = contact.ContactIdentity.Email, Mobile = contact.ContactIdentity.Mobile, PersonIdentity = (PersonIdentity)Enum.Parse(typeof(PersonIdentity), contact.ContactIdentity.PersonIdentity) }; repository.SaveStudentParent(other); //添加Parent信息 foreach (EasyChatTimeEntity item in contact.EasyChatTimes) { item.IfParentID = other.ParentID; repository.SaveEasyChatTime(item); //添加EasyChatTime信息 } }
public void SaveStudentParent(StudentParentEntity parent) { if (parent.ParentID == Guid.Empty) parent.ParentID = Guid.NewGuid(); if (context.StudentParent.SingleOrDefault(s => s.ParentID == parent.ParentID) == null) context.StudentParent.Add(parent); else { StudentParentEntity originParent = context.StudentParent.SingleOrDefault(s => s.ParentID == parent.ParentID); context.Entry(originParent).CurrentValues.SetValues(parent); } context.SaveChanges(); }