Asp.net 使用mvc3的代码优先方法

Asp.net 使用mvc3的代码优先方法,asp.net,asp.net-mvc-3,ef-code-first,Asp.net,Asp.net Mvc 3,Ef Code First,我想知道我是否在用好的方式做事。我有两种型号: 工作人员: 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 { ge

我想知道我是否在用好的方式做事。我有两种型号:

工作人员:

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; }
}
我使用迁移来生成表。我知道我做错了什么,因为当我向数据库添加一个新的
认证时,它会创建一个新的
RH_人员
,即使它已经存在

我的控制器:

 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>
}
@model Intra.Models.RH_认证
@使用(Html.BeginForm(“创建”、“认证”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(model=>model.Employe.Username)
@Html.HiddenFor(Model=>Model.Employe.RH_PersonnelID)

@Html.Label(“Nom”):
@EditorFor(model=>model.Employe.Nom)

@Html.Label(“Prénom”): @EditorFor(model=>model.Employe.Prenom)

@Html.DropDownList(“typedetection”、“Selectionner-un-type”)

}
在执行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();

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

发生的情况是,您正在创建一个新的
RH_认证
,并将其
Employe
属性设置为等于一个新的
Employe
。您要做的是检查
Employe
是否存在,如果存在,请调用
.Attach(Employe)
RH_人员
RH_认证
之间的关系是什么?关系是:单个
RH_认证
绑定到单个
RH_人员
一对一,所以这正是发生的事情,首先创建一个
RH_人员
,然后创建一个新的
RH_认证
,并将其
Employe
属性设置为等于一个新的
RH_人员
。根据应用程序的工作流程和您的
RH_人员的创建方法
有不同的方法来解决此问题。你能发布你用来创建一个新的
RH_人员
的代码吗?
.Attach(employe)
是解决方案。谢谢。它工作得很好!!这是一个好的做法还是只是一种变通方法?:)很乐意帮忙。我不能说这是一个好的做法,因为我不知道。我能说的是,这不是一个解决办法,因为上下文需要知道您正在使用的对象。否则,EF将把对象视为新对象。