所以我在使用Entity Framework并使用代码优先方法插入带有整数ID字段的记录时出现问题。当我尝试插入ID为0或ID为null的记录时,我得到以下异常
其他信息:存储更新,插入或删除语句会影响>意外行数(0)。由于>实体已加载,实体可能已被修改或删除。有关理解和处理乐观并发异常的信息,请参阅http://go.microsoft.com/fwlink/?LinkId=472540。
这是我控制器中的代码
// POST: Products/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Product_ID,PageURL,Name,Code,Description,Category_ID,Price,DateCreated,DateModified,Featured")] Product product)
{
product.DateCreated = DateTime.Now;
product.DateModified = DateTime.Now;
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Category_ID = new SelectList(db.Categories, "Category_ID", "CategoryName", product.Category_ID);
return View(product);
}
这是插入该实体的模型
namespace SeniorProjectMVC.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Product")]
public partial class Product
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Product()
{
OrderLines = new HashSet<OrderLine>();
SKU_Table = new HashSet<SKU_Table>();
XREF_CatalogProduct = new HashSet<XREF_CatalogProduct>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string FormattedPrice { get { return this.Price.ToString("C"); } }
//[Key]
[Required]
[MaxLength]
public string PageURL { get; set; }
[Required]
[StringLength(250)]
public string Name { get; set; }
[Required]
public string Code { get; set; }
public string Description { get; set; }
public int Category_ID { get; set; }
[Column(TypeName = "money")]
public decimal Price { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
[Required]
public bool Featured { get; set; }
public virtual Category Category { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<OrderLine> OrderLines { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<OrderLine> ProductImage { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SKU_Table> SKU_Table { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<XREF_CatalogProduct> XREF_CatalogProduct { get; set; }
}
}
现在我不知道这是否是有用的信息,但项目首先是错误的数据库,然后我把它改为代码,因为我认为这样会更容易混淆。我尝试了一些不同的东西,似乎无法解决这个问题。
更新:尝试从绑定语句中删除产品ID,但似乎仍无法解决问题。以下是绑定语句的更改内容。
public ActionResult Create([Bind(Include = "PageURL,Name,Code,Description,Category_ID,Price,DateCreated,DateModified,Featured")] Product product)
UPDATE 我添加了我的数据库架构的屏幕截图。我尝试将notmapped数据注释添加到该只读属性,如下面的注释中所述。它没有修复异常。我错过了什么吗?
更新:我试图确保我的架构匹配,我将数据注释添加到只读属性以指定它没有被映射,我仍然得到这个例外。我没有足够的声誉来为此付出赏金,我真的需要帮助解决这个问题。
答案 0 :(得分:1)
如果没有看到您的数据库架构,我会下注您的模型未正确映射到它所基于的表。你的财产
public string FormattedPrice { get { return this.Price.ToString("C"); } }
是派生属性,无法映射到数据库字段,但您没有告诉EF忽略它。尝试添加[NotMapped]注释。同样,检查所有属性是否实际上都有数据库中的相应字段。
如果您需要更多帮助来识别可能的问题,请随意发布用于创建的表DDL语句。