不能在ASP.Net MVC 3项目,实体框架的SaveChanges [英] Can't SaveChanges with Entity Framework in ASP.Net MVC 3 project
问题描述
学习asp.net MVC 3 + EF code-第一。我是新来的两个。我的例子很简单,但我仍然不能使它发挥作用。失去了一些东西简单明了...
Studying asp.net mvc 3 + EF code-first. I am new to both. My example is trivial, but I still can't make it work. Missing something simple and obvious...
我有一个类:
public class Product
{
[HiddenInput(DisplayValue = false)]
public int ProductID { get; set; }
[Required(ErrorMessage = "Please enter a product name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter a description")]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[Required]
[Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
public decimal Price { get; set; }
[Required(ErrorMessage = "Please specify a category")]
public string Category { get; set; }
}
和的DbContext
:
public class EFDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
和存储库:
public class EFProductRepository : IProductRepository
{
private EFDbContext context = new EFDbContext();
public IQueryable<Product> Products
{
get
{
return context.Products;
}
}
public void SaveProduct(Product product)
{
if (product.ProductID == 0)
context.Products.Add(product);
context.SaveChanges();
}
}
MVC的控制器:
The mvc controller:
public class AdminController : Controller
{
private IProductRepository repository;
public AdminController(IProductRepository repo)
{
repository = repo;
}
public ViewResult Index()
{
return View(repository.Products);
}
public ViewResult Edit(int productId)
{
Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId);
return View(product);
}
[HttpPost]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
repository.SaveProduct(product);
TempData["message"] = string.Format("{0} has been saved", product.Name);
return RedirectToAction("Index");
}
else
{
// there is something wrong with the data values
return View(product);
}
}
}
它让我看到的产品列表中,打开编辑视图,验证一切按属性集...
It lets me see the list of products, opens the edit view, validates everything according to the set of attributes...
当我保存验证它进入HTTP POST 修改
方法的变化并进行必要的的SaveChanges()
。
When I save validated changes it goes to the Http Post Edit
method and makes the necessary SaveChanges()
.
它不抛出任何异常,它的推移和重定向我的产品清单。
It doesn't throw any exceptions, it goes on and redirect me to the list of products.
编辑的项目保持不变。
的基础数据库(在到
)保持不变为好。的ConnectionStrings
连接的web.config
The underlying database (connected through connectionstrings
in web.config
) stays unchanged as well.
推荐答案
您需要附加EF之外创建实体实例,让EF知道它已被修改。
You need to attach the entity instance created outside EF and let EF know that it has been modified.
public void SaveProduct(Product product)
{
if (product.ProductID == 0)
{
context.Products.Add(product);
}
else
{
context.Products.Attach(product);
context.Entry(product).State = EntityState.Modified;
}
context.SaveChanges();
}
这篇关于不能在ASP.Net MVC 3项目,实体框架的SaveChanges的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!