实体框架代码首先 - 更新功能失败

时间:2012-06-21 14:52:58

标签: asp.net-mvc-2 entity-framework-4 entity-framework-4.1

内部异常消息:发生了引用完整性约束违规:定义引用约束的属性值在关系中的主体和从属对象之间不一致。

dynamic obj = "";
            if (model.LoadResultCount!=0)
            {
                obj = new LoadManagementResultsCountUserSetting();
                obj.ResultsCount = Convert.ToInt16(model.LoadResultCount);
                obj.Id = Convert.ToInt16(model.LoadResultCountId);
                //var loadManagementResultCount = ApplicationService.GetSettings<LoadManagementResultsCountUserSetting>(f => f.Id == model.LoadResultCountId).FirstOrDefault();
                //ApplicationService.Remove(loadManagementResultCount);
                SaveResultCountUserSettings(obj, model);

            }
            if (model.PlanningResultCount!=0)
            {
                obj = new PlanningManagementResultsCountUserSetting();
                obj.ResultsCount = Convert.ToInt16(model.PlanningResultCount);
                obj.Id = Convert.ToInt16(model.PlanningResultCountId);
                //var planningManagementResultCount = ApplicationService.GetSettings<PlanningManagementResultsCountUserSetting>().Where(f => f.Id == model.PlanningResultCountId).FirstOrDefault();
                //ApplicationService.Remove(planningManagementResultCount);
                SaveResultCountUserSettings(obj, model);
            }

   private void SaveResultCountUserSettings(dynamic obj, ResultCountViewModel model)
    {
        obj.IsEnabled = true;
        obj.IsPrimary = model.IsPrimary;
        obj.StartDate = DateTime.UtcNow;
        obj.ModifiedDate = DateTime.UtcNow;
        obj.ModifiedBy = AuthenticatedUser;
        ApplicationService.Save(obj);
    }

public void Save(BaseLogixSetting setting)
    {
        if (setting != null)
        {
            if (setting.Id > 0)
                SettingsRepository.Attach(setting);
            else
                SettingsRepository.Add(setting);

            Commit();
        }
    }

0 个答案:

没有答案