Пример #1
0
 public void TestInitialize()
 {
     _dbContext = new EFBlogContext();
     // EFBlogContext'i kullanıyor olduğumuz için EFUnitOfWork'den türeterek constructor'ına
     // ilgili context'i constructor injection yöntemi ile inject ediyoruz.
     _uow                = new EFUnitOfWork(_dbContext);
     _userRepository     = new EFRepository <User>(_dbContext);
     _categoryRepository = new EFRepository <Category>(_dbContext);
     _articleRepository  = new EFRepository <Article>(_dbContext);
 }
Пример #2
0
        public EFRepository(EFBlogContext dbContext)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException("dbContext can not be null.");
            }

            _dbContext = dbContext;
            _dbSet     = dbContext.Set <T>();
        }
        public EFUnitOfWork(EFBlogContext dbContext)
        {
            Database.SetInitializer <EFBlogContext>(null);

            if (dbContext == null)
            {
                throw new ArgumentNullException("dbContext can not be null.");
            }

            _dbContext = dbContext;

            // Buradan istediğiniz gibi EntityFramework'ü konfigure edebilirsiniz.
            //_dbContext.Configuration.LazyLoadingEnabled = false;
            //_dbContext.Configuration.ValidateOnSaveEnabled = false;
            //_dbContext.Configuration.ProxyCreationEnabled = false;
        }
Пример #4
0
        static void Main(string[] args)
        {
            using (var db = new EFBlogContext())
            {
                // Create
                Console.WriteLine("Inserting a new blog");
                db.Add(new Blog {
                    Url = "http://blogs.msdn.com/adonet"
                });
                db.SaveChanges();

                // Read
                Console.WriteLine("Querying for a blog");
                var blog = db.Blogs
                           .OrderBy(b => b.BlogId)
                           .First();

                // Update
                Console.WriteLine("Updating the blog and adding a post");
                blog.Url = "https://devblogs.microsoft.com/dotnet";
                blog.Posts.Add(
                    new Post
                {
                    Title   = "Hello World",
                    Content = "I wrote an app using EF Core!"
                });
                db.SaveChanges();

                //// Delete
                //Console.WriteLine("Delete the blog");
                //db.Remove(blog);
                //db.SaveChanges();

                Console.WriteLine($"Blog ID: {blog.BlogId} Blog URL: {blog.Url}");
                foreach (Post post in blog.Posts)
                {
                    Console.WriteLine($"Blog: {blog.BlogId} - Post: {post.PostId} - Title: {post.Title} Content: {post.Content}");
                }
            }
        }
 public void TestCleanup()
 {
     _dbContext = null;
     _uow.Dispose();
 }