/// <summary> /// 单实体表单保存 /// </summary> /// <param name="t">表单内容实体</param> /// <param name="form">表单实体</param> /// <returns></returns> public bool SaveData(T t, Form form) { CarlzhuContext carlzhuContext = ContextFactory.ContextHelper; using (carlzhuContext) { try { t.FormNo = form.FormNo; carlzhuContext.Forms.Add(form); carlzhuContext.Entry <T>(t).State = EntityState.Added; return(carlzhuContext.SaveChanges() >= 2); } catch (Exception exception) { DbEntityValidationException ex = exception as DbEntityValidationException; if (ex != null) { var entityError = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); string exceptionMessage = string.Concat(ex.Message, "errors are: ", string.Join("; ", entityError)); Console.Write(exceptionMessage); } Link.ErrorBy(exception, this.GetType()); Console.Write(exception); return(false); } } }
public virtual void Update(TaskViewModel task, ModelStateDictionary modelState) { if (ValidateModel(task, modelState)) { if (string.IsNullOrEmpty(task.Title)) { task.Title = ""; } var entity = task.ToEntity(); db.Tasks.Attach(entity); db.Entry(entity).State = EntityState.Modified; db.SaveChanges(); } }