来自WCFTestClient的对象持久性 - 实体框架

时间:2013-09-16 16:35:22

标签: c# wcf entity-framework persistence dbcontext

我正在通过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;
    }

当我执行与“Player”类构造函数(Player从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()呢?

0 个答案:

没有答案