Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET db.savechange System.Data.Entity.Infrastructure.DbUpdateException_C#_Asp.net_.net_Entity Framework_Concurrency - Fatal编程技术网

C# ASP.NET db.savechange System.Data.Entity.Infrastructure.DbUpdateException

C# ASP.NET db.savechange System.Data.Entity.Infrastructure.DbUpdateException,c#,asp.net,.net,entity-framework,concurrency,C#,Asp.net,.net,Entity Framework,Concurrency,我遇到了一个奇怪的情况 我收到以下错误消息: System.Data.Entity.Infrastructure.DbUpdateException:'错误 保存不公开外键的实体时发生 属性的关系。EntityEntries属性将 返回null,因为无法将单个实体标识为源 这是个例外。可以在保存时处理异常 通过在实体类型中公开外键属性更容易。看见 有关详细信息,请参阅InnerException 内部异常: OptimisticConcurrencyException:存储更新、插入或删除 语句

我遇到了一个奇怪的情况

我收到以下错误消息:

System.Data.Entity.Infrastructure.DbUpdateException:'错误 保存不公开外键的实体时发生 属性的关系。EntityEntries属性将 返回null,因为无法将单个实体标识为源 这是个例外。可以在保存时处理异常 通过在实体类型中公开外键属性更容易。看见 有关详细信息,请参阅InnerException

内部异常:

OptimisticConcurrencyException:存储更新、插入或删除 语句影响了意外的行数(0)。实体可能有 自加载实体后已被修改或删除。看见 有关 理解和处理乐观并发异常

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

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

   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 "";
    }
    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;
            }
        }
    } 
公共字符串OpenCards()
{
IsLogged=(System.Web.HttpContext.Current.User!=null)&&System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
如果(已记录)
CurrentUser=db.Users.Find(User.Identity.GetUserId());
其他的
返回“NotLoggedIn”;
如果(CurrentUser.CurrentRoom==null)
返回“UserNotConnectedToRoom”;
如果(CurrentUser.CurrentRoom.Round==0)
{
CurrentUser.CurrentRoom.SetAllPlayingThisRound(真);
//删除以前的CurrentUser.CurrentRoom.CardsDeck
CurrentUser.CurrentRoom.RemovePlayersCards(db);
如果(CurrentUser.CurrentRoom.CardsDeck!=null)
{
db.Cards.RemoveRange(CurrentUser.CurrentRoom.CardsDeck);
}
List List=RoomManager.FillDeck(CurrentUser.CurrentRoom);
CurrentUser.CurrentRoom.CardsDeck=列表;
CurrentUser.CurrentRoom.SendCardsToPlayers();
db.SaveChanges();//此处发生错误
返回“…”;
}
返回“”;
}
我在执行以下操作时收到错误消息:db.SaveChanges()

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

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

现在,从我在互联网上读到的内容来看,出现这种错误的原因有两个:

  • 正如它从错误本身所说的:与null有关的东西
  • 与并发相关的东西
  • 奇怪的是,这个错误并不是一直都发生,我无法重现它,也不知道它发生的原因

    因此,在尝试修复之前,我如何知道此错误的原因是否为1。还是2。?在调试模式下我应该寻找什么

    编辑:

    我的模型:

    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; }
    }
    
    公共类卡
    {
    公共int-CardId{get;set;}
    公共int值{get;set;}
    公共字符串类型{get;set;}
    }
    公共教室
    {
    public int RoomId{get;set;}
    公共字符串名称{get;set;}
    public int enterprise{get;set;}
    public int UserDecisionTime{get;set;}
    公共虚拟ICollection卡签{get;set;}
    公共虚拟ICollection CardsOnTable{get;set;}
    公共int WhosTurnChairIndex{get;set;}
    公共虚拟应用程序用户0{get;set;}
    公共虚拟应用程序用户主席1{get;set;}
    公共虚拟应用程序用户主席2{get;set;}
    公共虚拟应用程序用户主席3{get;set;}
    公共虚拟应用程序用户主席4{get;set;}
    公共int第一轮{get;set;}
    公共整数轮{get;set;}
    }
    
    以及来自SQL Server的一些卡数据:

    编辑:在@Train建议进行db.SaveChanges()之后,我注意到在每一行更改数据库之后,我都会收到一个错误:

    CurrentUser.CurrentRoom.SendCardsToPlayers()

    这就是方法:

       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 "";
        }
    
        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;
                }
            }
        } 
    
    public void SendCardsToPlayers()
    {
    对于(int i=0;i<5;i++)
    {
    ApplicationUser user=(ApplicationUser)GetProperty(该“椅子”+i);
    if(user!=null&&user.isplayingthis回合==true&&IsPlayerHasEnoughMoney(user))
    {
    卡c1=卡签第一次();
    拆下卡扣(c1);
    卡片c2=卡片签入。第一个();
    拆下卡扣(c2);
    user.CardOne=c1;
    user.cardtow2=c2;
    }
    else if(用户!=null)
    {
    user.isplayingthishall=false;
    }
    }
    } 
    
    此方法放置在房间的实体中

    另一个注释:我试图删除行:CardsDeck.remove(c1); 和卡扣。拆下(c2)

    但它仍然会犯这个错误

    所以问题出在:cardc1=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;
        }
    
    公共静态列表填充甲板(房间)
    {
    列表=新列表();
    字符串[]名称=新字符串[]
    {
    “梅花”、“钻石”、“心”、“黑桃”
    };
    //创建卡片组
    对于(int i=0;i<13;i++)
    {
    对于(int j=0;j<4;j++)
    {
    列表。添加(新卡)
    {
    值=i,
    类型=名称[j]
    });
    }
    }
    list.Shuffle();
    退货清单;
    }
    
    **另一个注意事项:*我发现如果我删除所有与卡片相关的内容,它们是: