Entity framework 实体框架在helper方法之后跟踪错误的模型对象

Entity framework 实体框架在helper方法之后跟踪错误的模型对象,entity-framework,asp.net-mvc-4,entity-framework-5,Entity Framework,Asp.net Mvc 4,Entity Framework 5,下面是一些基本代码,它是我们的Createcontroller方法的一部分。如果选中某个复选框,我们希望创建当前正在创建的agenttransmission对象的副本,并更改几个字段 当程序进入helper方法时,它会创建sub记录而不发生意外,但是由于某种原因,当程序完成并返回到Create方法时,模型对象agenttransmission成为sub模型对象。所有值,包括PK都突然出现在agenttransmission对象中 不确定这是如何发生的,因为helper方法上有一个字符串返回值,并

下面是一些基本代码,它是我们的
Create
controller方法的一部分。如果选中某个复选框,我们希望创建当前正在创建的
agenttransmission
对象的副本,并更改几个字段

当程序进入helper方法时,它会创建
sub
记录而不发生意外,但是由于某种原因,当程序完成并返回到
Create
方法时,模型对象
agenttransmission
成为
sub
模型对象。所有值,包括
PK
都突然出现在
agenttransmission
对象中

不确定这是如何发生的,因为helper方法上有一个
字符串
返回值,并且
agenttransmission
记录上没有涉及任何字段

创建方法

//Create substat if requested
if (agenttransmission.OverrideId)
{
    status += ". " + CreateSubStat(agenttransmission);
}
    public string CreateSubStat(AgentTransmission master)
    {
        string msg = string.Empty;
        if (ModelState.IsValid)
        {
            using (AgentResourcesEntities tempDb = new AgentResourcesEntities())
            {
                try
                {
                    //Check to see if substat already exists
                    var check = (from a in db.AgentTransmission
                                 where a.ParentId == master.ID && a.RecordStatus.Equals("P")
                                 select a).ToList();

                    if (check.Count > 0) return string.Empty;

                    //If not add dependent record
                    AgentTransmission sub = master;
                    sub.OverrideId = false;
                    sub.DRMCompanyId = string.Empty;
                    sub.CONCode = "00";
                    sub.RecordStatus = "P";
                    sub.ParentId = master.ID;
                    sub.ID = 0;
                    sub.IsSubstat = true;
                    sub.SendToDynamicsOptions = "N";
                    sub.SendToNMF = false;

                    //Remove blanks from ClearinghousePartners list
                    sub.ClearinghousePartners.RemoveAll(
                        x =>
                        string.IsNullOrWhiteSpace(x.ClearingHouseName) &&
                        string.IsNullOrWhiteSpace(x.TradingPartnerName) && x.StartDate == null);

                    sub.AgentRelationshipCodes.RemoveAll(
                        x =>
                        string.IsNullOrWhiteSpace(x.RelationshipId) &&
                        !x.EffectiveDate.HasValue && x.Id == 0);

                    foreach (var item in sub.AgentRelationshipCodes)
                    {
                        item.LastChangeDate = DateTime.Now;
                        item.LastChangeId = SecurityRules.GetUserName(User);
                        item.AgentTransmission = sub;
                        item.AgtTableId = sub.ID;
                    }

                    foreach (var item in sub.ClearinghousePartners)
                    {
                        item.AgentTransmission = sub;
                        item.AgtTransId = sub.ID;
                    }

                    db.AgentTransmission.Add(sub);
                    db.SaveChanges();

                    msg = "Substat saved with status of 'Dependent'.";
                }
                catch (DbEntityValidationException dbEx)
                {
                    msg = "Error creating substat. IT has been informed and will respond shortly.";
                    SendEmail.ErrorMail(dbEx.Message, SecurityRules.GetUserName(User));
                }
                catch (Exception ex)
                {
                    msg = "Error creating substat. IT has been informed and will respond shortly.";
                    SendEmail.ErrorMail(ex, SecurityRules.GetUserName(User));
                }
            }                
        }
        else
        {
            //Invalid ModelState error handling
            string messages = string.Join("; ", ModelState.Values
                                                          .SelectMany(x => x.Errors)
                                                          .Select(x => x.ErrorMessage));
            SendEmail.ErrorMail("Error Creating Substat: " + messages, SecurityRules.GetUserName(User));
            msg = "Error creating substat. IT has been informed and will respond shortly.";
        }

        return msg;
    }
助手方法

//Create substat if requested
if (agenttransmission.OverrideId)
{
    status += ". " + CreateSubStat(agenttransmission);
}
    public string CreateSubStat(AgentTransmission master)
    {
        string msg = string.Empty;
        if (ModelState.IsValid)
        {
            using (AgentResourcesEntities tempDb = new AgentResourcesEntities())
            {
                try
                {
                    //Check to see if substat already exists
                    var check = (from a in db.AgentTransmission
                                 where a.ParentId == master.ID && a.RecordStatus.Equals("P")
                                 select a).ToList();

                    if (check.Count > 0) return string.Empty;

                    //If not add dependent record
                    AgentTransmission sub = master;
                    sub.OverrideId = false;
                    sub.DRMCompanyId = string.Empty;
                    sub.CONCode = "00";
                    sub.RecordStatus = "P";
                    sub.ParentId = master.ID;
                    sub.ID = 0;
                    sub.IsSubstat = true;
                    sub.SendToDynamicsOptions = "N";
                    sub.SendToNMF = false;

                    //Remove blanks from ClearinghousePartners list
                    sub.ClearinghousePartners.RemoveAll(
                        x =>
                        string.IsNullOrWhiteSpace(x.ClearingHouseName) &&
                        string.IsNullOrWhiteSpace(x.TradingPartnerName) && x.StartDate == null);

                    sub.AgentRelationshipCodes.RemoveAll(
                        x =>
                        string.IsNullOrWhiteSpace(x.RelationshipId) &&
                        !x.EffectiveDate.HasValue && x.Id == 0);

                    foreach (var item in sub.AgentRelationshipCodes)
                    {
                        item.LastChangeDate = DateTime.Now;
                        item.LastChangeId = SecurityRules.GetUserName(User);
                        item.AgentTransmission = sub;
                        item.AgtTableId = sub.ID;
                    }

                    foreach (var item in sub.ClearinghousePartners)
                    {
                        item.AgentTransmission = sub;
                        item.AgtTransId = sub.ID;
                    }

                    db.AgentTransmission.Add(sub);
                    db.SaveChanges();

                    msg = "Substat saved with status of 'Dependent'.";
                }
                catch (DbEntityValidationException dbEx)
                {
                    msg = "Error creating substat. IT has been informed and will respond shortly.";
                    SendEmail.ErrorMail(dbEx.Message, SecurityRules.GetUserName(User));
                }
                catch (Exception ex)
                {
                    msg = "Error creating substat. IT has been informed and will respond shortly.";
                    SendEmail.ErrorMail(ex, SecurityRules.GetUserName(User));
                }
            }                
        }
        else
        {
            //Invalid ModelState error handling
            string messages = string.Join("; ", ModelState.Values
                                                          .SelectMany(x => x.Errors)
                                                          .Select(x => x.ErrorMessage));
            SendEmail.ErrorMail("Error Creating Substat: " + messages, SecurityRules.GetUserName(User));
            msg = "Error creating substat. IT has been informed and will respond shortly.";
        }

        return msg;
    }
那条线

AgentTransmission sub = master;
…不会将
master
复制到
sub
,而是将其分配给
sub
,因为
AgentTransmission
是一种引用类型。因此,您对
sub
所做的一切都是对
master
所做的

您必须将
master
克隆到
sub
(创建一个新对象并复制其属性),或者在调用
CreateSubStat
后从数据库重新获取master对象