EF Code First:该系列已被修改

时间:2012-03-27 14:07:04

标签: c# .net asp.net-mvc exception-handling ef-code-first

尝试将项添加到我的存储库/上下文时出现此错误:

  

收藏已被修改。可能不会执行库存操作。

我正在尝试做什么

Campaign c = Campaignrepository.FindById(id);
if (c.LandingPage == null)
{
    c.LandingPage = new Page() { Campaign = c, CampaignID = c.Campaignid };
    PageRepository.Add(c.LandingPage);
}
if (c.RedeemPage == null)
{
    c.RedeemPage = new RedeemPage() { Campaign = c, CampaignID = c.Campaignid };
    PageRepository.Add(c.RedeemPage);
}

存储库添加方法:

public void Add(Page p)
{
    pages.Add(p);
    context.SaveChanges();
}

更新:仍然是同样的错误。

应用建议后:

public ActionResult Edit(int id)
{
    Campaign c = Campaignrepository.FindById(id);
    Campaign newreferenceC = Campaignrepository.FindById(id);
    if (c.LandingPage == null)
    {
        c.LandingPage = new Page() { Campaign = newreferenceC, CampaignID = newreferenceC.Campaignid };
        PageRepository.Add(c.LandingPage);
    }
    if (c.RedeemPage == null)
    {
        c.RedeemPage = new RedeemPage() { Campaign = newreferenceC, CampaignID = newreferenceC.Campaignid };
        PageRepository.Add(c.RedeemPage);
    }
    return View("Edit", Campaignrepository.FindById(id));
}

1 个答案:

答案 0 :(得分:0)

管理以修复它:

        Page landingPage = new Page(c);
        RedeemPage redeemPage = new RedeemPage(c);
        PageRepository.Add(landingPage);
        PageRepository.Add(redeemPage);
        c.LandingPage = landingPage;
        c.RedeemPage = redeemPage;
        Campaignrepository.Update(c);