gpt4 book ai didi

c# - ASP.NET db.savechange System.Data.Entity.Infrastructure.DbUpdateException 异常

转载 作者:行者123 更新时间:2023-11-30 22:52:52 27 4
gpt4 key购买 nike

所以,我遇到了一个奇怪的情况,

我收到此错误消息:

System.Data.Entity.Infrastructure.DbUpdateException: 'An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.'

InnerException:

OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

我正在尝试使用 AJAX 和 .NET 构建纸牌游戏,

现在,我有一个 Method1 需要在 Room 实体中进行一些更改,这是该方法的完整代码:

   public string OpenCards()
{
IsLogged = (System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
if (IsLogged)
CurrentUser = db.Users.Find(User.Identity.GetUserId());
else
return "NotLoggedIn";

if (CurrentUser.CurrentRoom == null)
return "UserNotConnectedToRoom";

if (CurrentUser.CurrentRoom.Round == 0)
{
CurrentUser.CurrentRoom.SetAllPlayingThisRound(true);

// DELETE THE PREVIOUS CurrentUser.CurrentRoom.CardsDeck
CurrentUser.CurrentRoom.RemovePlayersCards(db);
if(CurrentUser.CurrentRoom.CardsDeck != null)
{
db.Cards.RemoveRange(CurrentUser.CurrentRoom.CardsDeck);
}

List<Card> list = RoomManager.FillDeck(CurrentUser.CurrentRoom);
CurrentUser.CurrentRoom.CardsDeck = list;

CurrentUser.CurrentRoom.SendCardsToPlayers();
db.SaveChanges(); // ERROR HAPPENS HERE


return "....";


}

return "";
}

我在执行以下操作时收到消息错误:db.SaveChanges()

基本上,从这段代码中需要知道的是我正在更改三个实体:ApplicationUser、Room 和 Cards。

现在,每隔几秒,就会调用Method2,它所做的基本上是显示来自数据库上下文的一些数据。

现在根据我在互联网上的阅读,出现此错误的原因有两个:

  1. 正如错误本身所说:与空值有关。
  2. 与并发相关的东西。

现在奇怪的是,这个错误并不是一直都在发生,而且我无法重现,也不知道为什么会发生。

所以在尝试修复它之前,我怎么知道这个错误的原因是 1. 还是 2.?我应该在 Debug模式下寻找什么?

编辑:

我的模型:

public class Card
{
public int CardId { get; set; }
public int Value { get; set; }
public string Type { get; set; }
}

public class Room
{
public int RoomId { get; set; }
public string Name { get; set; }
public int EnterPrice { get; set; }
public int UserDecisionTime { get; set; }
public virtual ICollection<Card> CardsDeck { get; set; }
public virtual ICollection<Card> CardsOnTable { get; set; }
public int WhosTurnChairIndex { get; set; }
public virtual ApplicationUser Chair0 { get; set; }
public virtual ApplicationUser Chair1 { get; set; }
public virtual ApplicationUser Chair2 { get; set; }
public virtual ApplicationUser Chair3 { get; set; }
public virtual ApplicationUser Chair4 { get; set; }
public int FirstRound { get; set; }
public int Round { get; set; }
}

还有一些来自SQL Server的卡片数据:

enter image description here

enter image description here

编辑:在@Train 建议在更改数据库的每一行之后执行 db.SaveChanges() 之后,我注意到我在这一行之后收到错误消息:

CurrentUser.CurrentRoom.SendCardsToPlayers();

方法是这样的:

    public void SendCardsToPlayers()
{
for (int i = 0; i < 5; i++)
{
ApplicationUser user = (ApplicationUser)GetProperty(this, "Chair" + i);

if (user != null && user.IsPlayingThisRound == true && IsPlayerHasEnoughMoney(user))
{
Card c1 = CardsDeck.First();
CardsDeck.Remove(c1);

Card c2 = CardsDeck.First();
CardsDeck.Remove(c2);

user.CardOne = c1;
user.CardTwo = c2;
}
else if (user != null)
{
user.IsPlayingThisRound = false;
}
}
}

这个方法放在Room的实体中。

另一个注意:我试图删除以下行:CardsDeck.Remove(c1);和 CardsDeck.Remove(c2);

但还是报这个错。

所以问题在:Card c1 = CardsDeck.First();

可能是因为我不是从 CardsDeck.ToList() 制作的,所以会出现问题?

编辑:根据要求,FillDeck 的方法:

    public static List<Card> FillDeck(Room room)
{
List<Card> list = new List<Card>();
string[] names = new string[]
{
"Club", "Diamond", "Heart", "Spade"
};

// Creating the Card deck

for(int i = 0; i < 13; i++)
{
for(int j = 0; j < 4; j++)
{
list.Add(new Card
{
Value = i,
Type = names[j]
});
}
}

list.Shuffle();
return list;
}

** 另一个注意事项:** 我发现如果删除与卡片相关的所有内容,它们是:

  • RoomManager.FillDeck(CurrentUser.CurrentRoom);
  • CurrentUser.CurrentRoom.SendCardsToPlayers(db);
  • db.Cards.RemoveRange(CurrentUser.CurrentRoom.CardsDeck.ToList());

没有问题,请问是不是Card实体的问题?我想到的另一个解决方案是将 Card 制作成字符串(用逗号分隔)或者甚至是字符串中的 JSON,这样它可能会解决整个 Cards 问题?

最佳答案

很难判断缺少某些方法的代码发生了什么。这是我认为的问题所在。

您的 Card 实体具有属性

{
CardID: ... //Primary Key
value: ...
Type: ...
CurrentRoom... //is this a foreign Key?
...//other properties
}

但是当您创建它时,您只有 2 个属性。

           list.Add(new Card
{
Value = i,
Type = names[j]
}); //no foriegn key or primary key defined

您不能对不完整的实体进行数据库操作。您还没有为卡片定义 Primaryforeign 键,然后当您执行所有数据库操作时,它不知道要对哪些卡片执行 crud 操作.

关于c# - ASP.NET db.savechange System.Data.Entity.Infrastructure.DbUpdateException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57733174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com