public T Create(T t) { try { var newEntry = DbSet.Add(t); context.SaveChanges(); return(newEntry); } catch (DbEntityValidationException e) { foreach (var error in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", error.Entry.Entity.GetType().Name, error.Entry.State); foreach (var ve in error.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
public void AddDocument(DocumentModel doc) { doc.Tables.Add(doc.HeadTable); Document dbDoc = new Document(); using (var db = new practiceEntities()) { db.Configuration.AutoDetectChangesEnabled = false; dbDoc.Acts = ConvertActsToDbActs(doc.Acts); dbDoc.Tables = ConvertToDbTables(doc.Tables); db.Documents.Add(dbDoc); db.ChangeTracker.DetectChanges(); db.SaveChanges(); } }
public void UpdateUserRecord(UserModel model) { using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required)) { User result = DB.Users.FirstOrDefault(u => u.UserId == model.UserId); if (result != null) { result.RoleId = model.RoleId; result.DesgId = model.DesignationId; result.OfficeId = model.OfficeId; result.Username = model.Username ?? result.Username; result.LoginId = model.LoginId ?? result.LoginId; result.UpdatedDate = model.UpdatedDate; result.IsActive = model.IsActive; result.EmailAddress = model.EmailAddress; } DB.Entry(result).CurrentValues.SetValues(result); DB.SaveChanges(); ts.Complete(); } }
public void UpdateFiscalYearRecord(FiscalYearModel model) { using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required)) { var fiscalYear = Db.FiscalYears.FirstOrDefault(x => x.Id == model.Id); if (fiscalYear != null) { fiscalYear.FiscalYearName = model.FiscalYearName ?? fiscalYear.FiscalYearName; fiscalYear.CreatedDate = model.CreatedDate ?? fiscalYear.CreatedDate; fiscalYear.UpdatedDate = model.UpdatedDate ?? fiscalYear.UpdatedDate; fiscalYear.StartDate = model.StartDate; fiscalYear.EndDate = model.EndDate; fiscalYear.StartDateNp = model.StartDateNp ?? fiscalYear.StartDateNp; fiscalYear.EndDateNp = model.EndDateNp ?? fiscalYear.EndDateNp; fiscalYear.CurrentFiscalYear = model.CurrentFiscalYear; } Db.Entry(fiscalYear).CurrentValues.SetValues(fiscalYear); Db.SaveChanges(); ts.Complete(); } }