0

我正在通过 WCFTestClient 测试我的 WCF 服务。我的方法为我运行并返回正确的值。但是,当我从 WCFTestClient 执行时,我的实体没有持久化。这是我正在测试的方法:

public PlayerActionResult DoAction(PlayerActionType type, int uid, string pparam = null, int amount = 0, string svalue = null) 
    {
        Player player;
        PlayerAction action = (PlayerAction)Activator.CreateInstance(null, string.Concat("Conquest.Players.Actions.", type.ToString())).Unwrap();
        action.Logic = (ActionLogic)Activator.CreateInstance(null, string.Concat("Conquest.Players.Actions.", type.ToString(), "Logic")).Unwrap();
        action.Logic.Action = action;
        // Verify executor exists
        if (!PlayerExists(uid))
        {
            return new PlayerActionResult(false, ResultType.NotFound);
        }
        else { player = GetPlayer(uid); }

        if (pparam != null & !PlayerExists(pparam))
        {
            if (!PlayerExists(pparam))
            {
                return new PlayerActionResult(false, ResultType.NotFound);
            }
            action.playerparam = GetPlayer(pparam);
        }
        PlayerActionResult result = player.DoAction(action);
        player.SaveChanges();
        return result;
    }

当我作为“播放器”类构造函数(播放器派生自 DbContext)的一部分执行相同类型的逻辑时,所做的更改将保存:

        public Player(int uid, string name)
    {
        this.Name = name;
        this.UserId = uid;
        this.Remorts = 0;
        this.Gender = Gender.Male;
        this.NumAttacks = 3;
        this.Movement = 30;
        this.PlayerClass = PlayerClass.Fighter;
        this.CurrentCont = Helper.GetRandom(3, 1);
        this.Kingdom = new HashSet<Kingdom>();
        this.Kingdom.Add(new Kingdom(this.CurrentCont));
        this.CurrentKingdom.Vault.AddItem(new Item(ItemType.LureVulture));
        this.CurrentKingdom.Vault.AddItem(new Item(ItemType.PotSpeed));
        PlayerAction action = (PlayerAction)Activator.CreateInstance(null, string.Concat("Conquest.Players.Actions.Pray")).Unwrap();
        action.Logic = (ActionLogic)Activator.CreateInstance(null, string.Concat("Conquest.Players.Actions.", "PrayLogic")).Unwrap();
        action.Logic.Action = action;
        this.DoAction(action);
}

谁能给我一些见解,为什么在 DbContext 派生对象的构造函数中调用此逻辑会保留数据并在外部调用逻辑然后使用 SaveChanges() 不会?

4

0 回答 0