EF实体类修改注意点

EF实体类修改之后,这个实体类在上下文中的状态会自动变成修改状态,如果在其他地方调用

base.DBAction.SaveChanges();那么,该实体类会直接被修改。

        /// <summary>
        /// 调整算法值
        /// </summary>
        /// <param name="algorithm"></param>
        /// <param name="isUseSolutionDay"></param>
        protected void AdjustAlgorithm(PolicyBaseAlgorithmEntity algorithm, bool isUseSolutionDay)
        {
            if (algorithm == null) return;
            if (_extraAlgorithms != null && _extraAlgorithms.Length > 0)
            {
                var extraAlgorithm = _extraAlgorithms.FirstOrDefault(p => p.ItemName == algorithm.InsuranceName);
                if (extraAlgorithm != null)
                {
                    if (extraAlgorithm.PersonalPercentage.HasValue && extraAlgorithm.PersonalPercentage.Value >= 0)
                    {
                        algorithm.PersonalPercentage = extraAlgorithm.PersonalPercentage.Value;
                    }
                    if (extraAlgorithm.EnterprisePercentage.HasValue && extraAlgorithm.EnterprisePercentage.Value >= 0)
                    {
                        algorithm.EnterprisePercentage = extraAlgorithm.EnterprisePercentage.Value;
                    }
                }
            }
            if (isUseSolutionDay && _insuranceSolutionEntity != null)
            {
                algorithm.AddDay = _insuranceSolutionEntity.AddDay;
                algorithm.StopDay = _insuranceSolutionEntity.StopDay;
            }
        }

应该修个成:

        /// <summary>
        /// 调整算法值
        /// </summary>
        /// <param name="algorithm"></param>
        /// <param name="isUseSolutionDay"></param>
        protected PolicyBaseAlgorithmEntity AdjustAlgorithm(PolicyBaseAlgorithmEntity algorithm, bool isUseSolutionDay)
        {
            if (algorithm == null) return null;
            var alg = new PolicyBaseAlgorithmEntity();
            algorithm.Copy(alg);        // 避免修改上下文,所以先拷贝
            if (_extraAlgorithms != null && _extraAlgorithms.Length > 0)
            {
                var extraAlgorithm = _extraAlgorithms.FirstOrDefault(p => p.ItemName == alg.InsuranceName);
                if (extraAlgorithm != null)
                {
                    if (extraAlgorithm.PersonalPercentage.HasValue && extraAlgorithm.PersonalPercentage.Value >= 0)
                    {
                        alg.PersonalPercentage = extraAlgorithm.PersonalPercentage.Value;
                    }
                    if (extraAlgorithm.EnterprisePercentage.HasValue && extraAlgorithm.EnterprisePercentage.Value >= 0)
                    {
                        alg.EnterprisePercentage = extraAlgorithm.EnterprisePercentage.Value;
                    }
                }
            }
            if (isUseSolutionDay && _insuranceSolutionEntity != null)
            {
                alg.AddDay = _insuranceSolutionEntity.AddDay;
                alg.StopDay = _insuranceSolutionEntity.StopDay;
            }
            return alg;
        }

 

posted @ 2015-10-28 18:48  江境纣州  阅读(22)  评论(0编辑  收藏  举报