【问题标题】:Code first approach with mvc3使用 mvc3 的代码优先方法
【发布时间】:2013-06-13 16:26:41
【问题描述】:

我想知道我是否以正确的方式做事。我有两个模型:

人员:

public class RH_Personnel
{
    public int RH_PersonnelID { get; set; }
    public string Nom { get; set; }
    public string Prenom { get; set; }
}

证明:

public class RH_Attestation
{
    public int RH_AttestationID { get; set; }
    public virtual RH_Personnel Employe { get; set; }
    public string TypeAttestation { get; set; }
}

我使用迁移来生成我的表。我知道我做错了什么,因为当我向数据库添加一个新的Attestation 时,它会创建一个新的RH_Personnel,即使它已经存在。

我的控制器:

 public ActionResult Create()
    {
        ViewBag.TypeAttestation = new SelectList(db.RH_TypeAttestation.ToList(),"Type","Type");
        RH_Attestation Attestation = new RH_Attestation();
        Attestation.Employe = (RH_Personnel)HttpContext.Session["Employe"];
        return View();
    } 

    //
    // POST: /Attestation/Create

    [HttpPost]
    public ActionResult Create(RH_Attestation rh_attestation)
    {
        if (ModelState.IsValid)
        {
            //rh_attestation.Employe = (RH_Personnel)HttpContext.Session["Employe"];
            //rh_attestation.DateDemande = DateTime.Now;
            //rh_attestation.DateValidation = rh_attestation.DateDemande;
            //rh_attestation.Etat = ATTESTATION_ETAT_ENCOURS;
            db.RH_Attestation.Add(rh_attestation);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(rh_attestation);
    }

我的看法:

 @model Intra.Models.RH_Attestation

 @using (Html.BeginForm("Create", "Attestation", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<div class="form_settings">
    @Html.HiddenFor(model => model.Employe.Username)
    @Html.HiddenFor(Model => Model.Employe.RH_PersonnelID)
    <p>
        <span>
            @Html.Label("Nom") :
        </span>
        @Html.EditorFor(model => model.Employe.Nom)
    </p>
<p>
        <span>
            @Html.Label("Prénom") :
        </span>
        @Html.EditorFor(model => model.Employe.Prenom)
    </p>
@Html.DropDownList("TypeAttestation", "Selectionner un type")
    <p style="padding-top: 15px;">
        <span>&nbsp;</span>
        <input type="submit" value="Envoyer" class="submit" />
    </p>

</div>
}

【问题讨论】:

  • 发生的事情是您正在创建一个新的RH_Attestation 并将其Employe 属性设置为等于一个新的Employe。您要做的是检查Employe 是否存在,如果存在,则调用.Attach(employe)RH_PersonnelRH_Attestation 之间的关系是什么?
  • 关系是:单个RH_Attestation绑定到单个RH_Personnel 一对一
  • 所以这正是发生的事情,您首先创建一个RH_Personnel,然后创建一个新的RH_Attestation,并将其Employe 属性设置为一个新的RH_Personnel。根据您的应用程序的工作流程和 RH_Personnel 的创建方法,有不同的方法可以解决此问题。您可以发布用于创建新RH_Personnel 的代码吗?
  • .Attach(employe) 是解决方案,谢谢。

标签: asp.net asp.net-mvc-3 ef-code-first


【解决方案1】:

您可以在执行 SaveChanges() 之前将 Employee 实例附加到上下文。

这样的事情应该可以工作;

RH_Personnel employee = (RH_Personnel)HttpContext.Session["Employe"];
db.RH_Personnel.Attach(employee);
rh_attestation.Employee = employee;
db.RH_Attestation.Add(rh_attestation);
db.SaveChanges();

我没有测试它,所以告诉我我们是否朝着正确的方向前进;)

【讨论】:

  • 感谢它工作得很好!!这是一个好的做法还是只是一种解决方法?
  • :) 很高兴为您提供帮助。我不能说这是一个好习惯,因为我不知道。我可以说这不是一种解决方法,因为上下文需要了解您正在使用的对象。否则 EF 会将对象视为新对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
  • 2012-05-08
相关资源
最近更新 更多