MVC 5编辑动作保存输入错误

时间:2016-11-17 14:49:01

标签: c# asp.net-mvc entity-framework linq file-upload

当我正在编辑"销售会员"并且我试图保存新文件路径/或旧文件路径,根据用户输入,它应该覆盖图像,或者它将使用存储在数据库中的先前图像。

但是,当我尝试修改实体状态时,在保存SQL Server中的更改之前,我收到错误:

  

类型' System.InvalidOperationException'的例外情况发生在EntityFramework.dll中但未在用户代码中处理。附加信息:附加类型' SalesMember'的实体。失败,因为同一类型的另一个实体已具有相同的主键值。使用'附加'方法或将实体的状态设置为“未更改”#39;或者'修改'如果图中的任何实体具有冲突的键值。这可能是因为某些实体是新的并且尚未收到数据库生成的键值。在这种情况下,请使用'添加'方法或“添加”#39;实体状态跟踪图形,然后将非新实体的状态设置为“未更改”。或者'修改'酌情。

我正在使用该主键修改SalesMember。其中,它在本地保存图像,但不会保存数据库行。

我的编辑行动-----

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include="SalesMemberID,FirstName,LastName,PageName,CellPhone,Email,InactiveRedirectTo,CustomText,Photo,Active, Position")] SalesMember salesmember, HttpPostedFileBase file)
    {
        SalesMember SalesPhoto = db.SalesMembers.Find(salesmember.SalesMemberID);

        salesmember.Photo = saveImage(file, salesmember, SalesPhoto);


        if (ModelState.IsValid)
        {
            salesmember.AddDate = SalesPhoto.AddDate;
            salesmember.UpdateDate = DateTime.Now;
            salesmember.IPAddress = Request.UserHostAddress;
            salesmember.AddUser = "admin";
            salesmember.UpdateUser = "admin";

            db.Entry(salesmember).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(salesmember);
    }

SalesPhoto保存功能 -

private string saveImage(HttpPostedFileBase file, SalesMember salesmember, SalesMember SalesPhoto = null)
    {
        string photo = "";
        string returnPhoto = "";

        //File blank
        if( file == null )
            //If File Path For that User Already In DB, return that File Path
            if(! String.IsNullOrEmpty(SalesPhoto.Photo))
                return "/Images/no-upload-icon.jpg"; 

        //If File ! empty, Save image uploaded

        photo = Server.MapPath("/Images/" + salesmember.PageName.ToString() + ".jpg");
        file.SaveAs(photo);
        returnPhoto = "/Images/" + salesmember.PageName.ToString() + ".jpg";

        return returnPhoto;
    }

我认为它与LINQ查询有关,就像我删除LINQ查询& saveImage方法只是用测试路径保存照片(即" /Images/foo.jpg"),它的工作原理。

提前致谢。

1 个答案:

答案 0 :(得分:0)

在Entity Framework中,内存中不能有两个具有相同主键的实体(相同类型)。 salesmemberSalesPhoto如果您希望在表单中使用AddDate编辑,则可以将其作为HiddenField包含在表单中,或者

  

我的建议是在视图中处理ViewModels而不是EF模型   忽略这些问题

 [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="SalesMemberID,FirstName,LastName,PageName,CellPhone,Email,InactiveRedirectTo,CustomText,Photo,Active, Position")] SalesMember salesmember, HttpPostedFileBase file)
{
    SalesMember SalesPhoto = db.SalesMembers.AsNoTracking().Find(salesmember.SalesMemberID);

    salesmember.Photo = saveImage(file, salesmember, SalesPhoto);


    if (ModelState.IsValid)
    {
        salesmember.AddDate = SalesPhoto.AddDate;
        salesmember.UpdateDate = DateTime.Now;
        salesmember.IPAddress = Request.UserHostAddress;
        salesmember.AddUser = "admin";
        salesmember.UpdateUser = "admin";

        db.Entry(salesmember).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(salesmember);
}