尝试将项目添加到数据库上下文时,我收到以下错误。我在Web API项目中使用Entity Framework。示例代码如下所示。
异常
System.Data.Entity.Infrastructure.DbUpdateException
HResult = 0x80131501消息=更新时发生错误 条目。有关详细信息,请参阅内部异常来源=的EntityFramework StackTrace:at System.Data.Entity.Internal.InternalContext.SaveChanges()at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()at System.Data.Entity.DbContext.SaveChanges()at WebApiSample.Controllers.OrdersController.Orders(订购neworder)中 C:\库\到位桶\ OtherProjects \ WebApiSample \ WebApiSample \ \控制器OrdersController.cs:行 173点 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor<> c__DisplayClass10.b__9(对象 instance,Object [] methodParameters)at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象 instance,Object [] arguments)at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2参数,CancellationToken 的CancellationToken)内部异常1:UpdateException:更新时发生错误 条目。有关详细信息,请参阅内部异常。
内部异常2:SqlException:无法将值NULL插入 列' OrderNumber',表 ' C:\ REPOSITORY \到位桶\ OTHERPROJECTS \ WEBAPISAMPLE \ WEBAPISAMPLE \ APP_DATA \ DATA.MDF.dbo.Orders&#39 ;; 列不允许空值。 INSERT失败。声明一直如此 终止。
此代码使用Web API中的POST方法编写:
[HttpPost]
public IHttpActionResult Orders(Order neworder)
{
if (neworder == null)
{
return BadRequest();
}
string uri = Url.Link("DefaultApi", new { id = neworder.OrderNumber });
try
{
using (OrderContext db = new OrderContext("ConnectionString"))
{
if (neworder == null)
{
return BadRequest();
}
db.Orders.Add(neworder);
db.SaveChanges();
}
}
catch (Exception ex)
{
throw ex;
}
return Created<Order>(uri, neworder);
}
Order
实体:
namespace WebApiSample.Models
{
public class Order
{
[Key]
public int OrderNumber { get; set; }
public Decimal OrderAmount { get; set; }
public DateTime OrderDate { get; set; }
public string CustomerName { get; set; }
public int OrderQty { get; set; }
[Column("ShippingStatusFlag")]
public string ShippingStatus { get; set; }
public virtual ICollection<OrderItems> Items { get; set; }
}
}
OrderItems
实体:
namespace WebApiSample.Models
{
[Table("OrderItems")]
public class OrderItems
{
[Key]
[ForeignKey("Order")]
[Column(Order =1)]
public int OrderNumber { get; set; }
[Key]
[Column(Order = 2)]
public int ItemID { get; set; }
[Key]
[Column(Order = 3)]
public int ItemSeq { get; set; }
public Decimal ItemPrice { get; set; }
public string ItemDescription { get; set; }
public int ItemQty { get; set; }
public virtual Order Order { get; set; }
}
}
数据在POST请求正文中传递
{
"OrderNumber": 9000,
"OrderAmount": 20,
"OrderDate": "2018-01-15T00:00:00",
"CustomerName": "TEST",
"OrderQty": 3,
"ShippingStatus": "N",
"Items": [
{
"OrderNumber": 9000,
"ItemID": 535,
"ItemSeq": 1,
"ItemPrice": 10,
"ItemDescription": "Plantronics Bluetooth Headset",
"ItemQty": 1,
"order": null
},
{
"OrderNumber": 9000,
"ItemID": 536,
"ItemSeq": 2,
"ItemPrice": 5,
"ItemDescription": "Yellow StickyNote 100ct",
"ItemQty": 1,
"order": null
},
{
"OrderNumber": 9000,
"ItemID": 601,
"ItemSeq": 3,
"ItemPrice": 5,
"ItemDescription": "Black Think Permanent Marker",
"ItemQty": 1,
"order": null
}
]
}
可能导致此问题的原因是什么?我在这里做错了什么?
答案 0 :(得分:4)
使用OrderNumber
DatabaseGeneratedAttribute
媒体资源
public class Order
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int OrderNumber { get; set; }
...
}
背景:当单个整数属性标记为Key
时,默认设置为DatabaseGeneratedOption.Identity
。使用此设置,EF假定数据库将负责分配新的主键值。因此,对于具有Added
状态的实体,键列将被忽略,不会传输到数据库。
这解释了为什么错误大约为NULL
值,因为该属性在代码中甚至无法为空。