C#& ASP.NET MVC - 保存对数据库的更改不会保存

时间:2018-03-17 11:03:09

标签: c# sql-server linq asp.net-mvc-5 ssms

我尝试使用两个函数编辑数据库中的某些数据。第一个函数有效,但每次我尝试第二个函数时,它实际上并没有将更改保存到数据库中。

有人会知道我做错了吗?

示例在不同的类中,但我认为这应该是足够的信息:

PcController.cs

    public ActionResult PokemonSteps(int id, int steps)
    {
        var user = _userRepo.getUser(User.Identity.Name);
            _userPokemonRepo.walkPokemon(id, steps, user); //<-- doesn't work
            _userRepo.takeSteps(user, steps); //<-- does work

            return RedirectToAction("Pokemon", new { id });
    }

UserPokemonRepo.cs

    public void walkPokemon(int id, int steps, USER user)
    {
        //get the pokemon data that needs to be edited
        var pokemon = new UserPokemonRepo().getPokemon(id);

        //make sure the pokemon belongs to the trainer, and has enough steps
        if (pokemon.CTid == user.Id)
        {
            if (user.Steps < steps)
            {
                steps = user.Steps.Value;
            }

            var maxExp = getMaxExp(pokemon.POKEMON_DATA.EXP_RATIO.ExpRatio);

            if (pokemon.Experience + steps > maxExp)
            {
                steps = maxExp - pokemon.Experience;
            }

            addExperience(pokemon, steps);
            addHappiness(pokemon, steps / 2500);
        }
    }

    public void addExperience(USER_POKEMON pokemon, int steps)
    {
        //add experience to the pokemon
        pokemon.Experience += steps;

        var maxExp = getMaxExp(pokemon.POKEMON_DATA.EXP_RATIO.ExpRatio);

        if (pokemon.Experience > maxExp)
        {
            pokemon.Experience = maxExp;
        }

        //save to the database
        PDCEntities.SaveChanges();
    }

    public void addHappiness(USER_POKEMON pokemon, int happiness)
    {
        //add happiness to the pokemon
        pokemon.Happiness += (happiness);

        if (pokemon.Happiness > Constants.MAXHAPPINESS)
        {
            pokemon.Happiness = Constants.MAXHAPPINESS;
        }

        //save to the database
        PDCEntities.SaveChanges();
    }

我已经在调试器中逐步完成了它并且值发生了变化 - 我假设它与我的函数有关,以及它们是如何嵌套的?

_userRepo.takeSteps(user, steps)userRepo.cs中执行此操作,并且工作正常

    public void takeSteps(USER user, int steps)
    {
        user.Steps -= steps;
        PDCEntities.SaveChanges();
    }

非常感谢任何帮助,谢谢

0 个答案:

没有答案