【发布时间】:2012-07-03 17:09:07
【问题描述】:
我在 Visual Studio 2010 中使用 asp.net mvc 3 和 EF 4.1,我是新手。
我对最后插入的 ID 有疑问。我的数据库中有两个表“points_Order”和“points_OrderDetails”。
db.Order.Add(po);
db.SaveChanges();
//second thing i have tried
var orderID = (from oi in db.Order
select oi.ID)
.Max(i => i);
// and this first thing
pod.OrderID = po.ID;
pod.OrderID = orderID;
db.OrderDetails.Add(pod);
db.SaveChanges();
上下文类:
public class points_Order {
[Key]
public int ID { get; set; }
public string Owner { get; set; }
public DateTime Date { get; set; }
public byte Status { get; set; }
public int Total { get; set; }
//public virtual points_OrderDetails OrderDetail { get; set; }
}
public class points_OrderDetails {
[Key]
public int OrderID { get; set; }
public string OrderType { get; set; }
public int Amount { get; set; }
public int ItemID { get; set; }
public int ItemRecord { get; set; }
public string ItemCode { get; set; }
public byte Plus { get; set; }
public byte Degree { get; set; }
public string Kind { get; set; }
public string Sex { get; set; }
public string Race { get; set; }
public string Tier { get; set; }
public string SetItem { get; set; }
public string SOX { get; set; }
//public Blues Blue { get; set; }
}
public DbSet<points_Order> Order { get; set; }
public DbSet<points_OrderDetails> OrderDetails { get; set; }
我收到错误“{”无法将值 NULL 插入列“OrderID”,表“db.dbo.points_OrderDetails”;列不允许空值。 INSERT 失败。\r\n语句已终止。"}"
我能做什么?我是不是做错了什么?
【问题讨论】:
标签: c# asp.net-mvc-3 entity-framework-4.1