我有一个包含客户销售的数据库。我正在尝试使用Entity Framework更新1条记录,但是当我检查数据库时,两条记录已更新
我的表称为Customers
,并具有以下两条记录:
NameID GenderID ItemID ShopID Cost
--------------------------------------------------------
587651 1 464 9 NULL
587651 1 512 9 NULL
我想更新我尝试使用Entity Framework做的第一条记录。
这是我的代码:
using (var context = new Customers())
{
var _customers = (from all in context.Customers
where (all.NameID == 587651) &&
(all.GenderID == 1) &&
(all.ItemID == 464) &&
(all.ShopID == 9)
select all).First();
_customers.Cost = 100;
context.SaveChanges();
}
保存更改后,我在数据库中得到以下内容:
NameID GenderID ItemID ShopID Cost
--------------------------------------------------------
587651 1 464 9 100
587651 1 512 9 100
知道发生了什么吗?
答案 0 :(得分:0)
好,由于上面的评论,我现在解决了这个问题。定义我的表的代码如下所示:
public partial class Customers
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int NameJID { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int GenderID { get; set; }
public int? ItemID { get; set; }
[Key]
[Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ShopID { get; set; }
public double? Cost { get; set; }
}
如您所见,主键仅包含NameID,GenderID和ShopID。还请注意 ItemID 如何可为空。我现在将代码更改为此:
public partial class Customers
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int NameJID { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int GenderID { get; set; }
[Key]
[Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ItemID { get; set; }
[Key]
[Column(Order = 3)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ShopID { get; set; }
public double? Cost { get; set; }
}