Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 实体框架,告诉上下文实体已更新_Asp.net Mvc 3_Entity Framework - Fatal编程技术网

Asp.net mvc 3 实体框架,告诉上下文实体已更新

Asp.net mvc 3 实体框架,告诉上下文实体已更新,asp.net-mvc-3,entity-framework,Asp.net Mvc 3,Entity Framework,我有一个场景,其中我有一个图像属性,它是产品实体的一部分 当允许用户通过MVC3屏幕编辑此产品时,图像属性显示如下: <div class="editor-label">Image</div> <div class="editor-field"> @if (Model.ProductItem.ImageData == null) { @:None } else

我有一个场景,其中我有一个图像属性,它是产品实体的一部分

当允许用户通过MVC3屏幕编辑此产品时,图像属性显示如下:

<div class="editor-label">Image</div>
    <div class="editor-field">
        @if (Model.ProductItem.ImageData == null)
        {
        @:None
        }
        else
        {
            <img alt="Product Image" width="125" height="125" 
            src="@Url.Action("GetImage", "Product", new { Model.ProductItem.ProductId     })" />
        }
    </div>

    <div>Upload new image: <input type="file" name="Image" /></div>
本质上,我正在强制更新我要更新的产品列表中的项目(ProCur是列表中的项目,product是我要保存的新编辑值)

我的问题是:(A)就使用上下文而言,这是最好的方法吗?以及(B)在UI中有更好的方法吗?也就是说,将新旧图像绑定在一起,以便模型自动拾取更改


谢谢你所做的看起来是正确的

其基本思想是,当您从服务器获取数据并将其返回到浏览器时,您正在使用的EF上下文将被关闭

当回复从浏览器返回时,您需要将数据与要更新的行连接起来

基于id读取行,更新相关字段,然后运行save changes,这是一种很好的方法

      if (context.Products.Local.Count() == 0)
            {
                Product procurr = context.Products
                    .Where(p => p.ProductId == product.ProductId)
                    .FirstOrDefault();

                context.Entry(procurr).CurrentValues.SetValues(product);    
            }