/// <summary> /// Метод добавляет тестовые специализации. /// </summary> /// <param name="pdbc">Контекст.</param> /// <param name="name">Логин исполнителя.</param> private int AddSpec(PostgreDbContext pdbc, string name) { ExecutorSpecialization[] aSpecies = new ExecutorSpecialization[] { new ExecutorSpecialization() { SpecName = "Специализация1" }, new ExecutorSpecialization() { SpecName = "Специализация2" } }; UserEntity oExecutor = pdbc.Users .Where(e => e.UserName .Equals(name)) .FirstOrDefault(); if (oExecutor.Specializations == null) { oExecutor.Specializations = aSpecies; } pdbc.SaveChanges(); UserEntity getExecutor = pdbc.Users .Where(e => e.UserName .Equals(name)) .FirstOrDefault(); return(getExecutor.Specializations.Count()); }
public void GetQuestionsTest() { DbContextOptions <PostgreDbContext> postgreOptions = new DbContextOptionsBuilder <PostgreDbContext>().UseInMemoryDatabase(databaseName: "GetQuestionsTest").Options; PostgreDbContext postgreContext = new PostgreDbContext(postgreOptions); QuestionEntity[] aQuestions = new[] { new QuestionEntity() { QuestionId = 1, QuestionText = "Что такое «Ставка» в терминах сервиса?", NumberQuestion = 1 }, new QuestionEntity() { QuestionId = 2, QuestionText = "Что такое «Гарантийный период» в терминах сервиса?", NumberQuestion = 2 } }; postgreContext.Questions.AddRange(aQuestions); postgreContext.SaveChanges(); int questionsCount = postgreContext.Questions.Count(); Assert.AreEqual(2, questionsCount); }
public ServiceResult SaveChanges() { //using (var transaction = _context.Database.BeginTransaction()) //{ var transaction = _context.Database.BeginTransaction(); try { foreach (var dbEntityEntry in _context.ChangeTracker.Entries <BaseEntities>().Where(x => x.State == EntityState.Modified).ToList()) { try { dbEntityEntry.Property <DateTime>("CreatedDate").IsModified = false; dbEntityEntry.Property <int>("CreatedUserId").IsModified = false; } catch (Exception ex) { //Ignored } } //Encrypt Attribute'üne sahip propertyleri encrypt eder //foreach (var entity in _context.ChangeTracker.Entries().Where(x => x.State != EntityState.Unchanged && x.State != EntityState.Deleted).ToList()) //{ // var propertyInfos = entity.Entity.GetType().GetProperties() // .Where(x => // x.GetCustomAttributes(true).Any(attr => attr.GetType() == typeof(EncryptAttribute)) && // x.PropertyType == typeof(string) // ); // foreach (var prop in propertyInfos) // { // prop.SetValue(entity.Entity, prop.GetValue(entity.Entity, null)?.ToString().Encrypt()); // } //} var affectedRow = _context.SaveChanges(); transaction.Commit(); return(Result.ReturnAsSuccess(null, affectedRow)); } catch (DbUpdateConcurrencyException ex) { // Update the values of the entity that failed to save from the store ex.Entries.Single().Reload(); //return Result.ReturnAsFail(AlertResource.ConcurrencyControl); return(Result.ReturnAsFail("")); } catch (Exception ex) { transaction.Rollback(); return(Result.ReturnAsFail()); // } } }
/// <summary> /// Добавляет тестовую информацию профиля юзера. /// </summary> /// <param name="context"></param> void AddProfileInfo(PostgreDbContext context) { UserEntity oUser = new UserEntity { //UserLogin = "******", //UserPassword = "******", //UserEmail = "*****@*****.**", //UserType = "Заказчик", //UserPhone = "8(985)-435-65-78", //Age = 23 }; context.Users.Add(oUser); context.SaveChanges(); }
/// <summary> /// Метод добавляет тестового исполнителя. /// </summary> /// <param name="pdbc">Контекст.</param> private string AddExecutor(PostgreDbContext pdbc) { UserEntity oExecutor = new UserEntity { UserName = "******", UserPassword = "******", Email = "*****@*****.**", UserRole = "E", PhoneNumber = "8(985)-435-65-78", Age = 23 }; pdbc.Users.Add(oExecutor); pdbc.SaveChanges(); return(oExecutor.UserName); }
/// <summary> /// Метод добавляет тестовые задания. /// </summary> void AddTask(PostgreDbContext context) { //TaskDto oTask1 = new TaskDto() { // OwnerId = 1, // TaskTitle = "test11", // TaskDetail = "test111", // TypeCode = "EJ6B3ABgoUuP6wT/rgOgqw==", // CategoryCode = "U4MuMGKPiE251VPwtbtMgg==", // SpecCode = "OVUW7v4Tu0WABpcJ3aN0rg==" //}; //TaskDto oTask2 = new TaskDto() { // OwnerId = 2, // TaskTitle = "test11111", // TaskDetail = "test11111", // TypeCode = "EJ6B3ABxoUuP6wT/rgOgqw==", // CategoryCode = "U4MuMGKPiEh51VPwtbtMgg==", // SpecCode = "OVUW1m4Tu0WABpcJ3aN0rg==" //}; //context.Tasks.AddRange(oTask1, oTask2); context.SaveChanges(); }
public void SaveChanges() { context.SaveChanges(); }
/// <summary> /// Метод добавляет 7 тестовых заданий, но должен вернуть последние 5. /// </summary> /// <param name="context"></param> void AddTask(PostgreDbContext context) { TaskEntity oTask1 = new TaskEntity() { OwnerId = "1", TaskTitle = "test11", TaskDetail = "test111", TypeCode = "EJ6B3ABgoUuP6wT/rgOgqw==", CategoryCode = "U4MuMGKPiE251VPwtbtMgg==", SpecCode = "OVUW7v4Tu0WABpcJ3aN0rg==" }; TaskEntity oTask2 = new TaskEntity() { OwnerId = "2", TaskTitle = "test11111", TaskDetail = "test11111", TypeCode = "EJ6B3ABxoUuP6wT/rgOgqw==", CategoryCode = "U4MuMGKPiEh51VPwtbtMgg==", SpecCode = "OVUW1m4Tu0WABpcJ3aN0rg==" }; TaskEntity oTask3 = new TaskEntity() { OwnerId = "3", TaskTitle = "test11111", TaskDetail = "test11111", TypeCode = "EJ6B3ABxoUuP6wT/rgOgqw==", CategoryCode = "U4MuMGKPiEh51VPwtbtMgg==", SpecCode = "OVUW1m4Tu0WABpcJ3aN0rg==" }; TaskEntity oTask4 = new TaskEntity() { OwnerId = "4", TaskTitle = "test11111", TaskDetail = "test11111", TypeCode = "EJ6B3ABxoUuP6wT/rgOgqw==", CategoryCode = "U4MuMGKPiEh51VPwtbtMgg==", SpecCode = "OVUW1m4Tu0WABpcJ3aN0rg==" }; TaskEntity oTask5 = new TaskEntity() { OwnerId = "5", TaskTitle = "test11111", TaskDetail = "test11111", TypeCode = "EJ6B3ABxoUuP6wT/rgOgqw==", CategoryCode = "U4MuMGKPiEh51VPwtbtMgg==", SpecCode = "OVUW1m4Tu0WABpcJ3aN0rg==" }; TaskEntity oTask6 = new TaskEntity() { OwnerId = "6", TaskTitle = "test11111", TaskDetail = "test11111", TypeCode = "EJ6B3ABxoUuP6wT/rgOgqw==", CategoryCode = "U4MuMGKPiEh51VPwtbtMgg==", SpecCode = "OVUW1m4Tu0WABpcJ3aN0rg==" }; TaskEntity oTask7 = new TaskEntity() { OwnerId = "7", TaskTitle = "test11111", TaskDetail = "test11111", TypeCode = "EJ6B3ABxoUuP6wT/rgOgqw==", CategoryCode = "U4MuMGKPiEh51VPwtbtMgg==", SpecCode = "OVUW1m4Tu0WABpcJ3aN0rg==" }; context.Tasks.AddRange(oTask1, oTask2, oTask3, oTask4, oTask5, oTask6, oTask7); context.SaveChanges(); }
public async Task <Note> Create(Note model) { db.Notes.Add(model); db.SaveChanges(); return(await db.Notes.Where(i => i.Id == model.Id).FirstOrDefaultAsync()); }