DbContext.SaveChanges

时间:2012-08-08 14:30:27

标签: asp.net entity-framework

我有一个问题,即向实体插入一条记录ProfessionalEmploymentRecord EF最终会分别复制每个权利中的现有专业,计划,角色记录。我试图用我的数据库中的现有键/值对明确地初始化Professional,Program,Role,但EF仍然使用新键创建重复值。

public class ProfessionalEmploymentRecord
{
    public int ProfessionalEmploymentRecordId {get; set; }

    public virtual Professional Professional { get; set; }
    public virtual Program Program { get; set; }
    public virtual Role Role { get; set; }
}
public class Program
{
    public int ProgramId { get; set; }
    [MaxLength(20)]
    public string Name { get; set; }
}
public class Role
{
    public int RoleId { get; set; }
    [MaxLength(50)]
    public string Name { get; set; }
}
public class Professional
{
    public int ProfessionalId { get; set; }
    [MaxLength(20)]
    public string Name { get; set; }
}

当使用下面的Create时,我在三个相关实体中结束了新记录,尽管它们存在且具有密钥集。换句话说,我最终会在三个相关实体中使用新主键(递增1)的值副本。

    public ActionResult Create(ProfessionalEmploymentRecord professionalemploymentrecord)
    {
        if (ModelState.IsValid)
        {
            db.ProfessionalEmploymentRecords.Add(professionalemploymentrecord);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }
        return View(professionalemploymentrecord);
    }

根据要求

@model My.Models.ProfessionalEmploymentRecord
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend> Professional Employment Record</legend>

    <div class="editor-label">
        Professional
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Professional.Name)
        @Html.ValidationMessageFor(model => model.Professional.Name)
    </div>
    <div class="editor-label">
         Program
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Program.Name)
        @Html.ValidationMessageFor(model => model.Program.Name)
    </div>
    <div class="editor-label">
         Role
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Role.Name)
        @Html.ValidationMessageFor(model => model.Role.Name)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

我使用下面的代码进行测试。 (我看到执行SaveChanges设置* Id ++ in reco)。

    public void TestDummyAdd()
    {
        Professional prof = new Professional { ProfessionalId = 1, Name = "Chris" };
        Program prog = new Program { ProgramId = 1, Name = "A" };
        Role role = new Role { RoleId = 1, Name = "B" };
        ProfessionalEmploymentRecord reco = new ProfessionalEmploymentRecord { Professional = prof, Role = role, Program = prog, ProfessionalEmploymentRecordId = 0 };

        if (ModelState.IsValid)
        {
            db.ProfessionalEmploymentRecords.Add(reco);
            db.SaveChanges();
        }
    }

1 个答案:

答案 0 :(得分:1)

添加是在整个对象图上执行的操作。如果你添加你的记录,你也会添加它的所有关系,它们的关系等。有两个选项可以解决这个问题,并且都需要你手动设置持久化对象grap中某些实体(有时还有关系)的状态:

  • 按照您的方式调用Add,但之后您必须将图表中所有现有实体的状态设置为UnchangedModified(实体将在数据库中更新)
  • 调用Attach代替Add,然后调用所有新实体的设置状态Added

设置状态可以通过以下方式完成:

db.Entry(entity).State = EntityState.Added;