C# 实体框架不将我的数据保存到sql server

C# 实体框架不将我的数据保存到sql server,c#,asp.net,asp.net-mvc,entity-framework,linq-to-entities,C#,Asp.net,Asp.net Mvc,Entity Framework,Linq To Entities,我有下面的代码。它使用实体框架(5.0)来处理sql server(2008)的数据输入/输出。遗憾的是,修改后的数据无法成功保存到数据库。任何帮助我的人都会非常感激的 @model mvcapapplication1.Models.ProductEditViewModel @{ 布局=空; } 产品编辑 @使用(Html.BeginForm()) { @Html.AntiForgeryToken() 产品 @Html.ValidationSummary(true,“,new{@class

我有下面的代码。它使用实体框架(5.0)来处理sql server(2008)的数据输入/输出。遗憾的是,修改后的数据无法成功保存到数据库。任何帮助我的人都会非常感激的



@model mvcapapplication1.Models.ProductEditViewModel
@{
布局=空;
}
产品编辑
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
产品

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @Html.HiddenFor(model=>model.Product.ProductID) @LabelFor(model=>model.Product.ProductName,htmlAttributes:new{@class=“control label col-md-2”}) @Html.EditorFor(model=>model.Product.ProductName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Product.ProductName,“,新{@class=“text danger”}) @LabelFor(model=>model.Product.SupplierID,“SupplierID”,htmlAttributes:new{@class=“controllabel col-md-2”}) @*DropDownList用于播放目的*@ @*@DropDownList(“SupplierID”,Model.SupplierItems,htmlAttributes:new{@class=“form control”})*@ @*DropDownListFor用于编辑目的*@ @DropDownListFor(model=>model.Product.SupplierID,model.SupplierItems,htmlAttributes:new{@class=“form control”}) @LabelFor(model=>model.Product.CategoryID,“CategoryID”,htmlAttributes:new{@class=“controllabel col-md-2”}) @*RadioButton用于显示*@ @*@foreach(Model.CategorieItems中的var分类) { @Html.RadioButton(“CategoryID”,Categorie.CategoryID,Model.CategorySelectedId==Categorie.CategoryID)@Categorie.CategoryName;@: }*@ @*RadioButton用于编辑目的*@ @foreach(Model.CategorieItems中的var分类) { @Html.radio按钮(model=>model.Product.CategoryID,Categorie.CategoryID)@Categorie.CategoryName;@: } @LabelFor(model=>model.Product.UnitPrice,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Product.UnitPrice,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Product.UnitPrice,“,new{@class=“text danger”}) @LabelFor(model=>model.Product.contracted,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Product.contracted) @Html.ValidationMessageFor(model=>model.Product.contracted,“,new{@class=“text danger”}) } @ActionLink(“返回列表”、“索引”)


您的问题是您试图通过与您更新的上下文不同的上下文进行更新。以下代码就是问题所在:

var vm = new ProductEditViewModel(ProductId);
UpdateModel(vm.Product, "Product");

var northwind = new NorthwindEntities();
northwind.SaveChanges();
将在vm的上下文中更新模型。该行:

var northwind = new NorthwindEntities()

创建尚未编辑的新上下文。您可以将保存/更新方法添加到ProductEditViewModel(结构不好),也可以创建上下文,获取vm实例,更新它,然后保存它。

问题是我试图通过与我更新的上下文不同的上下文进行更新。感谢BBlake先生发现问题。正确的代码如下:




@model mvcapapplication1.Models.ProductEditViewModel
@{
布局=空;
}
产品编辑
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
产品

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @Html.HiddenFor(model=>model.Product.ProductID) @LabelFor(model=>model.Product.ProductName,htmlAttributes:new{@class=“control label col-md-2”}) @Html.EditorFor(model=>model.Product.ProductName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Product.ProductName,“,新{@class=“text danger”}) @LabelFor(model=>model.Product.SupplierID,“SupplierID”,htmlAttributes:new{@class=“controllabel col-md-2”}) @*DropDownList用于播放目的*@ @*@DropDownList(“SupplierID”,Model.SupplierItems,htmlAttributes:new{@class=“form control”})*@ @*DropDownListFor用于编辑目的*@ @DropDownListFor(model=>model.Product.SupplierID,model.SupplierItems,htmlAttributes:new{@class=“form control”}) @LabelFor(model=>model.Product.CategoryID,“CategoryID”,htmlAttributes:new{@class=“controllabel col-md-2”}) @*RadioButton用于显示*@ @*@foreach(Model.CategorieItems中的var分类) { @Html.RadioButton(“CategoryID”,Categorie.CategoryID,Model.CategorySelectedId==Ca
@model MvcApplication1.Models.ProductEditViewModel

@{
    Layout = null;
}

<script type="text/javascript">

</script>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ProductEdit</title>
</head>
<body>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>


    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <h4>Product</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.Product.ProductID)

            <div class="form-group">
                @Html.LabelFor(model => model.Product.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Product.ProductName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Product.ProductName, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Product.SupplierID, "SupplierID", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*DropDownList is for deiplay purpose*@
                    @*@Html.DropDownList("SupplierID", Model.SupplierItems, htmlAttributes: new { @class = "form-control" })*@

                    @*DropDownListFor is for Edit purpose*@
                    @Html.DropDownListFor(model => model.Product.SupplierID, Model.SupplierItems, htmlAttributes: new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Product.CategoryID, "CategoryID", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*RadioButton is for Display purpose*@
                    @*@foreach (var Categorie in Model.CategorieItems)
                        {
                            @Html.RadioButton("CategoryID", Categorie.CategoryID, Model.CategorySelectedId == Categorie.CategoryID) @Categorie.CategoryName; @:&nbsp;&nbsp;&nbsp;
                        }*@

                    @*RadioButtonFor is for Edit purpose*@
                    @foreach (var Categorie in Model.CategorieItems)
                    {
                        @Html.RadioButtonFor(model => model.Product.CategoryID, Categorie.CategoryID) @Categorie.CategoryName; @:&nbsp;&nbsp;&nbsp;
                    }

                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Product.UnitPrice, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Product.UnitPrice, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Product.UnitPrice, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Product.Discontinued, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <div class="checkbox">
                        @Html.EditorFor(model => model.Product.Discontinued)
                        @Html.ValidationMessageFor(model => model.Product.Discontinued, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
</body>
</html>
var vm = new ProductEditViewModel(ProductId);
UpdateModel(vm.Product, "Product");

var northwind = new NorthwindEntities();
northwind.SaveChanges();
var northwind = new NorthwindEntities()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
    public class Repository
    {
        public NorthwindEntities northwind;

        public Repository()
        {
            northwind = new NorthwindEntities();
        }

        public Product GetProductById(Int32 ProductId)
        {
            return (from pro in northwind.Products
                    where pro.ProductID == ProductId
                    select pro).FirstOrDefault();
        }

        public void Save()
        {
            this.northwind.SaveChanges();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Mvc;

namespace MvcApplication1.Models
{
    public class ProductEditViewModel
    {
        public Repository repository = new Repository();

        public Product Product;

        public ProductEditViewModel()
        {
            ;
        }

        // For DropDownListFor need IEnumerable<SelectListItem>
        public IEnumerable<SelectListItem> SupplierItems
        {
            get
            {
                var q = from sup in repository.northwind.Suppliers.AsEnumerable()
                        select new SelectListItem
                            {
                                Text = sup.CompanyName,
                                Value = sup.SupplierID.ToString(),
                                Selected = sup.SupplierID == Product.SupplierID
                            };

                return q;
            }
        }

        // For RadioButtonFor need below
        public IEnumerable<Category> CategorieItems
        {
            get
            {
                var q = from cat in repository.northwind.Categories.AsEnumerable() select cat;
                return q;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Web.Script.Serialization;
using MvcApplication1.Models;
using System.Data.Objects.SqlClient;
using System.Data;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        ProductEditViewModel vm = new ProductEditViewModel();

        // sample for populate sql server database table via entity framework
        // 1. Install EntityFramework 5.0, if previous sample for using populate oracle database table via entity framework has been installed, this can be skiped : execute command at Package Manager Console "Install-Package EntityFramework -Version 5.0.0" ( Oracle data access provider ODAC 12.1.0.1.2 current is not support (work with) EntityFramework version 6 ). If you want uninstall EntityFramework 5, using command "unInstall-Package EntityFramework -Version 5.0.0"
        // 2. Models => Add => New item => Data => Ado.Net Entity Data Model ( using EntityFramework 5.0 )
        public ActionResult ProductList()
        {
            var northwind = new NorthwindEntities(); // 3. this is connectionStrings name="NorthwindEntities"

            var q = from pro in northwind.Products
                    select new ProductListViewModel
                    {
                        ProductId = pro.ProductID,
                        ProductName = pro.ProductName,
                        Supplier = pro.Supplier.CompanyName,
                        Categorie = pro.Category.CategoryName,
                        Discontinued = pro.Discontinued,
                        UnitPrice = pro.UnitPrice
                    };

            return View(q.ToList());
        }

        [HttpGet]
        public ActionResult ProductEdit(Int32 ProductId)
        {
            vm.Product = vm.repository.GetProductById(ProductId);
            return View(vm);
        }

        [HttpPost]
        public ActionResult ProductEdit(Int32 ProductId, FormCollection fc)
         {
            vm.Product = vm.repository.GetProductById(ProductId);
            UpdateModel(vm.Product, "Product");
            vm.repository.Save();
            return RedirectToAction("ProductList");
        }
    }
}
@model MvcApplication1.Models.ProductEditViewModel

@{
    Layout = null;
}

<script type="text/javascript">

</script>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ProductEdit</title>
</head>
<body>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>


    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <h4>Product</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.Product.ProductID)

            <div class="form-group">
                @Html.LabelFor(model => model.Product.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Product.ProductName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Product.ProductName, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Product.SupplierID, "SupplierID", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*DropDownList is for deiplay purpose*@
                    @*@Html.DropDownList("SupplierID", Model.SupplierItems, htmlAttributes: new { @class = "form-control" })*@

                    @*DropDownListFor is for Edit purpose*@
                    @Html.DropDownListFor(model => model.Product.SupplierID, Model.SupplierItems, htmlAttributes: new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Product.CategoryID, "CategoryID", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*RadioButton is for Display purpose*@
                    @*@foreach (var Categorie in Model.CategorieItems)
                        {
                            @Html.RadioButton("CategoryID", Categorie.CategoryID, Model.CategorySelectedId == Categorie.CategoryID) @Categorie.CategoryName; @:&nbsp;&nbsp;&nbsp;
                        }*@

                    @*RadioButtonFor is for Edit purpose*@
                    @foreach (var Categorie in Model.CategorieItems)
                    {
                        @Html.RadioButtonFor(model => model.Product.CategoryID, Categorie.CategoryID) @Categorie.CategoryName; @:&nbsp;&nbsp;&nbsp;
                    }

                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Product.UnitPrice, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Product.UnitPrice, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Product.UnitPrice, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Product.Discontinued, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <div class="checkbox">
                        @Html.EditorFor(model => model.Product.Discontinued)
                        @Html.ValidationMessageFor(model => model.Product.Discontinued, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
</body>
</html>