我要使用EF更新一对一关系表,以更新被提名人的名单。最好的方法是什么。这是我的课程和代码
[Table("tbl_MemberProfile")]
public class MemberProfile : BaseTrackable
{
public string LandlineNumber { get; set; }
public string ProfilePicture { get; set; }
public bool isActive { get; set; }
public string ThumbImpression { get; set; }
public string ScannedCnic { get; set; }
public virtual ICollection<MemberNominee> ListMemberNominee { get; set; }
}
[Table("tbl_MemeberNominee")]
public class MemberNominee : BaseTable
{
public string CNIC { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string PresentAddress { get; set; }
[ForeignKey("MemberProfile")]
public long MemeberId { get; set; }
public virtual MemberProfile MemberProfile { get; set; }
}
public int UpdateMember(MemberProfileModel model)
{
try
{
using (var scope = new TransactionScope())
{
var _memberData = _db.MemberProfile.Where(x => x.ID == model.ID).Include(x => x.ListMemberNominee).FirstOrDefault();
if (_memberData != null)
{
_memberData.LandlineNumber = model.LandlineNumber;
_memberData.MobileNumber = model.MobileNumber;
_memberData.ScannedCnic = model.ScannedCnic;
_memberData.ThumbImpression = model.ThumbImpression;
_db.SaveChanges();
foreach (var item in model.MemberNomineeList)
{
MemberNominee _nominee = new MemberNominee
{
CNIC = item.CNIC,
FirstName = item.FirstName,
MiddleName = item.MiddleName,
LastName = item.LastName,
PresentAddress = item.PresentAddress,
MemeberId = _memberData.ID,
RelationId = item.RelationId
};
}
_db.SaveChanges();
scope.Complete();
return 1;
}
else { return -2; }
}
}
catch (Exception ex)
{
ExceptionLoggerHelper.SendExcepToDB(ex, model.UserId, "MemberService", "UpdateMember", "");
return -1;
}
}
在我的updateMember方法中,我要针对memberprofile表更新memberprofile表,我也想更新membernominee表。 MemberProfileModel具有来自用户端的新列表。如何在Entityframwork中使用最佳方法更新记录