0

我有一个问题,将一条记录插入实体 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; }
}

当使用下面的创建时,我最终会在三个相关实体中得到新记录,尽管它们存在并且有一个键集。换句话说,我最终在三个相关实体中得到了具有新主键(加一)的值副本。

    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 会在 reco 中设置 *Id++)。

    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();
        }
    }
4

1 回答 1

1

添加是对整个对象图执行的操作。如果您添加您的记录,您还将添加它的所有关系、它们的关系等。有两种方法可以解决此问题,并且都需要您在持久对象图谱中手动设置某些实体(有时还包括关系)的状态:

  • 像你一样调用Add,但之后你必须将图中所有现有实体的状态设置为Unchangedor Modified(实体将在数据库中更新)
  • 调用Attach而不是Add在所有新实体的设置状态之后调用Added

设置状态可以通过:

db.Entry(entity).State = EntityState.Added;
于 2012-08-08T15:01:48.707 回答