Asp.net mvc 3 为什么ctx.SaveChanges()不';t保存mvc 3和x2B中的更改;代码优先?
没有验证异常,但不保存更改,为什么 搜索SO后,添加ctx.Configuration.ValidateOnSaveEnabled=false;在添加之前,它可以工作 这是我的poco课程:Asp.net mvc 3 为什么ctx.SaveChanges()不';t保存mvc 3和x2B中的更改;代码优先?,asp.net-mvc-3,entity-framework-4,code-first,Asp.net Mvc 3,Entity Framework 4,Code First,没有验证异常,但不保存更改,为什么 搜索SO后,添加ctx.Configuration.ValidateOnSaveEnabled=false;在添加之前,它可以工作 这是我的poco课程: [HttpPost] public virtual ActionResult CreateProductVariant(ProductVariant pv) { try { ctx.ProductVariants.Add(pv)
[HttpPost]
public virtual ActionResult CreateProductVariant(ProductVariant pv)
{
try
{
ctx.ProductVariants.Add(pv);
ctx.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbvalationEx)
{
throw;
}
return RedirectToAction("Index");
}
公共类产品
{
//其他属性
公共ICollection产品变量{get;set;}
公共ICollection ProductImages{get;set;}
}
公共类ProductVariant
{
//其他属性
公共虚拟产品产品{get;set;}
}
没有例外,只是重定向到操作(“索引”);添加对象时是否指定键值?您在数据库中的密钥是标识字段吗?如果是,您是否指定了它?我在添加pv时没有指定键值
public class Product
{
//other properties
public ICollection<ProductVariant> ProductVariants { get; set; }
public ICollection<ProductImage> ProductImages { get; set; }
}
public class ProductVariant
{
//other properties
public virtual Product Product { get; set; }
}