实体框架6中未更改的对象

时间:2014-09-30 14:05:53

标签: c# entity-framework wcf

我有silverlight应用程序。 这是一个带有Entity Framework 6的调用操作(在Open Ria Service - WCF Ria Service开源版本中)。

    public List<VaultAmount> GetCurrentVaultAmounts(Guid vaultId)
    {
        return this.DbContext.VaultAmounts
            .Include(v => v.VaultAmountQuantities)
            .Include(v => v.VaultCurrency)
            .Include(v => v.Vault)
            .Include(v => v.VaultAmountQuantities.Select(vaq => vaq.VaultCurrency))
            .Where(v => v.VaultId == vaultId && v.IsCurrent).ToList();
    }

    [Invoke]
    public void UpdateVaultRemainders(List<VaultAmountQuantity> updatedQuantities, string comment, Guid userId,
        string friendlyName,
        Guid vaultAmountId, int currencyId)
    {
        VaultAmount vaultAmount =
            this.DbContext.VaultAmounts
                .Include("Vault")
                .SingleOrDefault(va => va.VaultAmountId == vaultAmountId);

        if (vaultAmount == null && vaultAmount.Vault == null) return;

        //Get FromVault and ToVault with amounts and updatedQuantities
        List<VaultAmount> currentAmounts = GetCurrentVaultAmounts(vaultAmount.Vault.VaultId);
        //Vault vault = GetVaultWithCurrentAmountsAndQuantitiesById(vaultAmount.Vault.VaultId);

        var helper = new RemainderVAHelper(currentAmounts, userId, friendlyName, currencyId, updatedQuantities,
            comment,
            BS2VaultEventTypes.Correction);

        //................................

        foreach (var amount in currentAmounts)
        {
            if (amount.IsCurrent == false)
            {
                DbEntityEntry<VaultAmount> entityEntry = this.DbContext.Entry(amount);
                entityEntry.State = EntityState.Modified;
            }
        }
        this.DbContext.SaveChanges();
    }

RemainderVAHelper 类中,我正在更改 currentAmounts 对象,将 IsCurrent 属性设置为false。 但 currentAmounts 对象 entityEntry.State 仍然未更改。为什么?是的,我可以将其状态设置为修改(就像我在方法中所做的那样),但我认为这不是一件好事。你能告诉我为什么我的对象状态没有变为修改吗?

1 个答案:

答案 0 :(得分:0)

如果您没有使用更改跟踪代理,EF无法知道您修改了属性,直到您调用DetectChanges或调用SaveChanges(调用DetectChanges)