public void CanAddEnityWithCustomKeyInIdentityWithIDENTITY_INSERT() { Employee emp = new Employee() { EmployeeId = 10, EmployeeName = "First" }; using (var ctx = new AppContextTest(true)) { ctx.Employees.Add(emp); ctx.Database.OpenConnection(); ctx.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Employees ON"); ctx.SaveChanges(); ctx.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Employees OFF"); } Employee emp2; using (var ctx = new AppContextTest()) { emp2 = ctx.Employees.FirstOrDefault(e => e.EmployeeId == 10); } Assert.NotNull(emp2); Assert.Equal(10, emp2.EmployeeId); Assert.Equal("First", emp2.EmployeeName); }
public void CanNotUpdateWhenAddedEntityWithSameKey() { var emp1 = new Employee() { EmployeeName = "First" }; using (var ctx = new AppContextTest(true)) { ctx.Employees.Add(emp1); ctx.SaveChanges(); } using (var ctx = new AppContextTest()) { ctx.Employees.Add(new Employee { EmployeeId = emp1.EmployeeId }); ctx.Database.OpenConnection(); ctx.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Employees ON"); //ctx.SaveChanges(); Assert.Throws <DbUpdateException>(() => ctx.SaveChanges()); ctx.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Employees OFF"); } }
public void CanNotAutoIncludeInjectEnity() { var order = new Order { Status = "Status 1",//Ручная установка одинакового значения в оба экземпляра обязательна OrderDetail = new OrderDetail { Address = "Address 1", Status = "Status 1" } }; using (var ctx = new AppContextTest(true)) { ctx.Orders.Add(order); ctx.SaveChanges(); } Order savedOrder = null; using (var ctx = new AppContextTest()) { savedOrder = ctx.Orders.Where(o => o.Id == order.Id).Single(); } Assert.Null(savedOrder.OrderDetail); using (var ctx = new AppContextTest()) { savedOrder = ctx.Orders.Include(o => o.OrderDetail).Where(o => o.Id == order.Id).Single(); } Assert.NotNull(savedOrder.OrderDetail); Assert.Equal("Address 1", savedOrder.OrderDetail.Address); }
public void CanConversion() { var periods = new ComparsionList <Period> { new Period { Day = 1 }, new Period { Day = 2 }, new Period { Day = 3 } }; var shedule = new Shedule { Periods = periods }; using (var ctx = new AppContextTest(true)) { ctx.Shedules.Add(shedule); ctx.SaveChanges(); } Assert.NotEqual(default, shedule.Id);
private void Seed() { using (var context = new AppContextTest()) { var i = 1; context.Database.EnsureDeleted(); context.Database.EnsureCreated(); var one = new Item("ItemOne"); one.AddTag("Tag11"); one.AddTag("Tag12"); one.AddTag("Tag13"); var two = new Item("ItemTwo"); var three = new Item("ItemThree"); three.AddTag("Tag31"); three.AddTag("Tag31"); three.AddTag("Tag31"); three.AddTag("Tag32"); three.AddTag("Tag32"); context.AddRange(one, two, three); context.SaveChanges(); } }
public void CanChangeOwnedPropety() { var delivery1 = new Delivery(); var a1 = new Address { Street = "Street 1" }; var a2 = new Address { Street = "Street 2" }; int a1Id = 0; int a2Id = 0; var addresses = new List <Address> { a1, a2 }; delivery1.Addresses = addresses; using (var ctx = new AppContextTest(true)) { ctx.Deliveries.Add(delivery1); ctx.SaveChanges(); a1Id = (int)ctx.Entry(delivery1.Addresses[0]).Property("Id").CurrentValue; a2Id = (int)ctx.Entry(delivery1.Addresses[1]).Property("Id").CurrentValue; } Assert.NotEqual(default, delivery1.DeliveryId);
public void CanLoadDerivedType() { var student = new Student { Name = "Student 1" }; var school = new School { Name = "School 1", Students = new List <Student> { student } }; var person = new Person { Name = "Person 1" }; using (var ctx = new AppContextTest(true)) { ctx.Schools.Add(school); ctx.People.Add(person); ctx.SaveChanges(); } School savedSchool = null; Student savedStudent = null; using (var ctx = new AppContextTest()) { savedSchool = ctx.Schools.Include(s => s.Students).Where(s => s.Id == school.Id).Single(); savedStudent = ctx.People.Include("School").Where(s => s.Id == student.Id).Single() as Student; } Assert.NotNull(savedSchool); Assert.Single(savedSchool.Students); Assert.Equal(savedSchool, savedStudent.School); Assert.Equal(student.Id, savedStudent.Id); }
public void CanNotChangeAlternateKeyAfterCreate() { var emp = new Employee { EmployeeName = "First", PassportId = "First_PassportId" }; using (var ctx = new AppContextTest(true)) { ctx.Employees.Add(emp); ctx.SaveChanges(); } Assert.NotEqual(default, emp.EmployeeId);
public void CanNotAddEnityWithCustomKeyInIdentity() { Employee emp = new Employee() { EmployeeId = 10, EmployeeName = "First" }; using (var ctx = new AppContextTest(true)) { ctx.Employees.Add(emp); Assert.Throws <DbUpdateException>(() => ctx.SaveChanges()); } }
public void CanOperationWithoutLoad() { var student1 = new Student { Name = "Student 1" }; var student2 = new Student { Name = "Student 2" }; var student31 = new Student { Name = "Student 31" }; var school = new School { Name = "School 1", Students = new List <Student> { student1, student2, student31 } }; var person = new Person { Name = "Person 1" }; using (var ctx = new AppContextTest(true)) { ctx.Schools.Add(school); ctx.People.Add(person); ctx.SaveChanges(); } School savedSchool = null; using (var ctx = new AppContextTest()) { savedSchool = ctx.Schools.Where(s => s.Id == school.Id).Single(); savedSchool.Students = null; var countStudents = ctx.Entry(savedSchool).Collection(s => s.Students).Query().Where(s => s.Name.Contains("1")).Count(); Assert.Equal(2, countStudents); Assert.Empty(savedSchool.Students);//При операциях явной загрузки без загрузки данных(например Count), коллекция будет создана без элемнтов } using (var ctx = new AppContextTest()) { savedSchool = ctx.Schools.Where(s => s.Id == school.Id).Single(); savedSchool.Students = null; var countStudents = ctx.People.Count(); Assert.NotEqual(default, countStudents);
public void HasEqualGeneratedId() { var order = new Order { Status = "Status 1",//Ручная установка одинакового значения в оба экземпляра обязательна OrderDetail = new OrderDetail { Address = "Address 1", Status = "Status 1" } }; using (var ctx = new AppContextTest(true)) { ctx.Orders.Add(order); ctx.SaveChanges(); } Assert.Equal(order.Id, order.OrderDetail.Id); Assert.Equal(order.Status, order.OrderDetail.Status); }
public void CanNullRefernceWithoutCascade() { var blog = new Blog { BlogName = "Name 1" }; var post1 = new Post { PostName = "Post 1", Blog = blog }; var post2 = new Post { PostName = "Post 2", Blog = blog }; using (var ctx = new AppContextTest(true)) { ctx.Blogs.Add(blog); ctx.Posts.AddRange(post1, post2); ctx.SaveChanges(); } Assert.NotEqual(default, post1.PostId);