示例#1
0
        public void CanIncludeThenIncludeNavigationProperties()
        {
            using (var db = new EF7BloggContext(ServiceProvider))
            {
                //EnsureCleanDatabase(db);

                db.Blogs.Add(new Blog()
                {
                    Name = "ADO.NET Blog"
                });
                db.Blogs.Add(new Blog()
                {
                    Name = "Visual Studio"
                });
                db.Blogs.Add(new Blog()
                {
                    Name = ".NET Blog"
                });

                db.SaveChanges();

                var seachedBlogs = db.Set <Blog>()
                                   .Include((b => b.Posts)) // Eager loading, in ef6 10 blog with 10 posts each
                                   .Where(b => b.Name.Contains("blog"));

                foreach (var blog in seachedBlogs)
                {
                    var found = blog.Name;
                }
            }
        }
示例#2
0
        public void CanPerformSomeInMermoryAndSomeInDatabase()
        {
            using (var db = new EF7BloggContext(ServiceProvider))
            {
                //EnsureCleanDatabase(db);

                db.Blogs.Add(new Blog()
                {
                    Name = "ADO.NET Blog"
                });
                db.Blogs.Add(new Blog()
                {
                    Name = "Visual Studio"
                });
                db.Blogs.Add(new Blog()
                {
                    Name = ".NET Blog"
                });

                db.SaveChanges();

                var seachedBlogs = db.Set <Blog>()
                                   .OrderBy(b => GetSortName(b.Name))
                                   .Where(b => b.Name.Contains(("blog"))); // Some parts of the query in db (where) and some inmemory (GetSortName)

                foreach (var blog in seachedBlogs)
                {
                    var found = blog.Name;
                }
            }
        }
        public void CanSelectDynamic()
        {
            using (var db = new EF7BloggContext())
            {
                var authorToSerachFor = Fixture.Create <string>();
                var blog = new Blog()
                {
                    Name = Fixture.Create <string>(), Description = Fixture.Create <string>()
                };

                db.AddRange(new[] { blog });

                db.SaveChanges();

                var retrievedBlog = db.Set <Blog>()
                                    .Include(p => p.Posts)
                                    .ToList();
                //.Select(p => new { p.Author })
                //.ToList();
                //.SelectDynamic(new[] { nameof(Blog.Author) });

                foreach (var blog1 in retrievedBlog)
                {
                    Assert.IsNotNull(blog1);
                }
            }
        }
        public void CanNotCrash()
        {
            //var blog = new Blog()
            //{
            //    Author = Fixture.Create<string>(),
            //    Name = Fixture.Create<string>(),
            //    Posts = new System.Collections.Generic.List<Post>()
            //    {
            //        new Post() { Date = "20150909", Text = Fixture.Create<string>() },
            //        //new Post() { Date = "20150909", Text = Fixture.Create<string>() }
            //    }
            //};

            var post = new Post()
            {
                Text = Fixture.Create <string>(),
                Url  = Fixture.Create <string>(),
            };

            using (var db = new EF7BloggContext())
            {
                db.ChangeTracker.TrackGraph(post, c => c.Entry.State = EntityState.Added);

                db.SaveChanges();
            };
        }
        public void CanRetrieveAndAllNavigationPropertiesArePopulated()
        {
            var blog = new Blog()
            {
                Author = Fixture.Create <string>(),
                Name   = Fixture.Create <string>(),
                Posts  = new System.Collections.Generic.List <Post>()
                {
                    new Post()
                    {
                        Date = "20150909", Text = Fixture.Create <string>(), Url = Guid.NewGuid().ToString()
                    },
                    new Post()
                    {
                        Date = "20150909", Text = Fixture.Create <string>(), Url = Guid.NewGuid().ToString()
                    }
                }
            };

            using (var db = new EF7BloggContext())
            {
                db.ChangeTracker.TrackGraph(blog, c => c.Entry.State = EntityState.Added);

                db.SaveChanges();
            };

            using (var db = new EF7BloggContext())
            {
                var retrievedBlog = db
                                    .Blogs.Include(b => b.Posts)
                                    .Single(b => b.Id == blog.Id);
            };
        }
示例#6
0
        public void AddDisconnected()
        {
            /*
             * Purpose: To test adding a simple object with no relations, in the first session using one context
             *       Later add a relation to object using another session (context)
             */

            Blog blog = null;

            using (var con = new EF7BloggContext(ServiceProvider))
            {
                blog = Fixture.Build <Blog>()
                       .OmitAutoProperties()
                       .With(p => p.Author, Fixture.Create <string>())
                       .With(p => p.Name, Fixture.Create <string>())
                       .With(p => p.Description, Fixture.Create <string>())
                       .Create();

                con.ChangeTracker.TrackGraph(blog, e => e.Entry.State = EntityState.Added);

                con.SaveChanges();
            }

            using (var con = new EF7BloggContext(ServiceProvider))
            {
                var post = Fixture.Build <Post>()
                           .OmitAutoProperties()
                           .With(p => p.Text, Fixture.Create <string>())
                           .With(p => p.Date, Fixture.Create <string>())
                           .With(p => p.Url, Fixture.Create <string>())
                           .With(p => p.Blog, blog)
                           .Create();

                con.ChangeTracker.TrackGraph(post, e =>
                {
                    if (e.Entry.IsKeySet)
                    {
                        e.Entry.State = EntityState.Modified;
                    }
                    else
                    {
                        e.Entry.State = EntityState.Added;
                    }
                });

                con.SaveChanges();
            }
        }
示例#7
0
        public void AddSingleObject()
        {
            /*
             * Purpose: To test adding one single object to the database, without navigation properties
             */
            using (var con = new EF7BloggContext(ServiceProvider))
            {
                var blog = Fixture.Build <Blog>()
                           .OmitAutoProperties()
                           .With(p => p.Author, Fixture.Create <string>())
                           .With(p => p.Name, Fixture.Create <string>())
                           .With(p => p.Description, Fixture.Create <string>())
                           .Create();

                con.Blogs.Add(blog);

                con.SaveChanges();
            }
        }
示例#8
0
        public void AddComplexObject()
        {
            /*
             * Purpose: To test adding object with one relation (navigation property Posts)
             */
            using (var con = new EF7BloggContext(ServiceProvider))
            {
                var blog = Fixture.Build <Blog>()
                           .OmitAutoProperties()
                           .With(p => p.Author, Fixture.Create <string>())
                           .With(p => p.Name, Fixture.Create <string>())
                           .With(p => p.Description, Fixture.Create <string>())
                           .With(p => p.Posts, Fixture.CreateMany <Post>().ToList())
                           .Create();

                con.ChangeTracker.TrackGraph(blog, e => e.Entry.State = EntityState.Added);

                con.SaveChanges();
            }
        }
示例#9
0
        public void TestEf7SqlGenerationImprovements()
        {
            using (var db = new EF7BloggContext(ServiceProvider))
            {
                //EnsureCleanDatabase(db);

                // EF6 three roundtrips to db
                db.Blogs.Add(new Blog()
                {
                    Name = "ADO.NET Blog"
                });
                db.Blogs.Add(new Blog()
                {
                    Name = "Visual Studio"
                });
                db.Blogs.Add(new Blog()
                {
                    Name = ".NET Blog"
                });

                db.SaveChanges();
            }
        }
示例#10
0
        public void CanRetriveUsingTableValuedFunction()
        {
            /*
             *          USE [EF7]
             *          GO
             *
             *          SET ANSI_NULLS ON
             *          GO
             *
             *          SET QUOTED_IDENTIFIER ON
             *          GO
             *
             *          DROP FUNCTION[dbo].[SearchBlogs];
             *
             *          GO
             *          CREATE FUNCTION[dbo].[SearchBlogs]
             *          (
             *                  @param2 nvarchar(200)
             *          )
             *          RETURNS @returntable TABLE
             *          (
             *                  Id int,
             *                  Author nvarchar(200),
             *                  Name nvarchar(200),
             *  Description nvarchar(200)
             *          )
             *          AS
             *          BEGIN
             *                  INSERT @returntable
             *                  SELECT
             *      blogs.Id, blogs.Name, blogs.Author, blogs.Description
             *  from dbo.Blog blogs
             *  where
             *      blogs.Name like @param2
             *                  RETURN
             *          END
             */

            using (var db = new EF7BloggContext(ServiceProvider))
            {
                //EnsureCleanDatabase(db);

                db.Blogs.Add(new Blog()
                {
                    Name = "ADO.NET Blog"
                });
                db.Blogs.Add(new Blog()
                {
                    Name = "Visual Studio"
                });
                db.Blogs.Add(new Blog()
                {
                    Name = ".NET Blog"
                });

                db.SaveChanges();

                /*
                 * 1. Parameterized, not open to sql injection
                 * 2. Strongly typed (renaming properties)
                 * 3. The order by is happening in the database, not in memory.
                 */
                var nameToSerachFor = "blog";
                var seachedBlogs    = db.Set <Blog>()
                                      .FromSql("select * from dbo.SearchBlogs (@p0)", nameToSerachFor);
                //.OrderBy(b => b.Name); // <-- Strongly typed

                foreach (var blog in seachedBlogs)
                {
                    var found = blog.Name;
                }
            }
        }
示例#11
0
 private void EnsureCleanDatabase(EF7BloggContext con)
 {
     con.Database.EnsureDeleted();
     con.Database.EnsureCreated();
 }