Example #1
0
        static void Main(string[] args)
        {
            var options   = SqliteInMemory.CreateOptions <TestModelContext>();
            var dbContext = new TestModelContext(options);


            dbContext.Database.EnsureCreated();

            //create book with one page and one image ( image has navigation props to book and bookpage)
            var book  = CreateBasicBook();
            var page  = CreateBasicBookPage(book);
            var image = CreateBasicImage(book, page);

            dbContext.Add(book);
            dbContext.Add(page);
            dbContext.Add(image);
            dbContext.SaveChanges();

            //take existing book
            var bookToCopy = dbContext.Books.Include(b => b.BookPages).Include(b => b.Images).First() as Book;

            //reset Connection to Database
            dbContext.Dispose();
            dbContext = new TestModelContext(options);

            // set keyproperties to zero
            bookToCopy.Id = 0;
            //set keyproperties of children to zero
            foreach (var i in bookToCopy.Images)
            {
                i.Id = 0;
            }
            foreach (var p in bookToCopy.BookPages)
            {
                p.Id = 0;
            }

            //add to context
            dbContext.Add(bookToCopy);
            //save context
            dbContext.SaveChanges();

            //reset Connection to Database changes everything
            //dbContext.Dispose();
            //dbContext = new TestModelContext(options);

            //output
            var allBooks = dbContext.Books.Include(b => b.BookPages).Include(b => b.Images).ToList();

            Console.WriteLine("Initial Book: " + JsonConvert.SerializeObject(allBooks[0], new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            }));
            Console.WriteLine("Copied Book: " + JsonConvert.SerializeObject(allBooks[1], new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            }));
        }
        public void DeleteCommandTest(bool isAdmin)
        {
            using (var modelContext = new TestModelContext()) {
                var entity = new DivisionInfo {
                    DivisionID = 1
                };
                modelContext.Add(entity);

                var securityContext = new TestSecurityContext(1, isAdmin);
                var command         = new DeleteCommand <DivisionInfo> (modelContext, securityContext);

                command.Delete(entity);

                Assert.Equal(isAdmin, null == modelContext
                             .QueryOne <DivisionInfo> (d => d.DivisionID == entity.DivisionID)
                             .SingleOrDefault());
            }
        }