Exemplo n.º 1
0
        public void CanUseExplicitTransactions()
        {
            //Arrange
            using (var ctx = new BlogContext(Configuration["Data:Blog:ConnectionString"]))
                using (var tx = ctx.Database.BeginTransaction(IsolationLevel.Serializable))
                {
                    var blog1 = new Blog
                    {
                        Name         = "Blog 1",
                        CreationDate = DateTime.Today
                    };
                    var blog2 = new Blog
                    {
                        Name         = "Blog 2",
                        CreationDate = DateTime.Today
                    };

                    ctx.AddRange(blog1, blog2);

                    try
                    {
                        //Act
                        ctx.SaveChanges();
                        tx.Commit();
                    }
                    catch
                    {
                        tx.Rollback();
                    }

                    //Assert
                    Assert.True(true);
                }
        }
Exemplo n.º 2
0
        public async Task Save(SettingModel model)
        {
            using (var tran = await BlogContext.Database.BeginTransactionAsync())
            {
                var settings = await BlogContext.Settings.ToListAsync();

                BlogContext.RemoveRange(settings);
                await BlogContext.SaveChangesAsync();

                var entityList = model
                                 .Settings
                                 .Where(w => w.Value != null)
                                 .Select(t => new Setting
                {
                    Key   = t.Key,
                    Value = t.Value
                });
                BlogContext.AddRange(entityList);

                await BlogContext.SaveChangesAsync();

                tran.Commit();

                RemoveCache();
            }
        }
Exemplo n.º 3
0
        public async void GetsAll()
        {
            var expected = new List <Post>
            {
                new Post
                {
                    Id        = 1,
                    Title     = "Pride and Prejudice",
                    Content   = @"It is a truth universally acknowledged, that a single man in possession of a good fortune,
                        must be in want of a wife.",
                    Author    = "Jane Austen",
                    CreatedOn = new DateTime(1813, 1, 28, 2, 0, 0)
                },
                new Post
                {
                    Id        = 2,
                    Title     = "Moby-Dick",
                    Content   = "Call me Ishmael.",
                    Author    = "Herman Melville",
                    CreatedOn = new DateTime(1854, 11, 14, 16, 30, 0)
                }
            };

            _blogContext.AddRange(expected);
            _blogContext.SaveChanges();

            var actual = await _entityRepository.GetAllAsync();

            actual.Should().BeEquivalentTo(expected);
        }
Exemplo n.º 4
0
            public void Insert_with_explicit_with_default_keys()
            {
                using (var context = new BlogContext())
                {
                    context.Database.EnsureClean();

                    context.AddRange(
                        new NullableKeyBlog {
                        Id = 0, Name = "One Unicorn"
                    },
                        new NullableKeyBlog {
                        Id = 1, Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContext())
                {
                    var blogs = context.NullableKeyBlogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(0, blogs[0].Id);
                    Assert.Equal(1, blogs[1].Id);
                }
            }
Exemplo n.º 5
0
 /// <summary>
 /// Sync Post Tags from UI
 /// </summary>
 /// <param name="postTags">Post Tags to Sync</param>
 public void Sync(List <PostTagEntity> postTags)
 {
     if (postTags != null && postTags.Count > 0)
     {
         _blogContext.PostTags.RemoveRange(_blogContext.PostTags.Where(a => a.PostId == postTags.FirstOrDefault().PostId));
         _blogContext.AddRange(postTags);
         _blogContext.SaveChanges();
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Add settings
        /// </summary>
        /// <param name="model"></param>
        private void AddSettings(InstallModel model)
        {
            SettingModel settingModel = new SettingModel(new Dictionary <string, string>(), SettingModelLocalizer);

            settingModel.Title    = model.BlogTitle;
            settingModel.Host     = model.BlogHost;
            settingModel.Language = model.Language;

            var settingList = settingModel.Settings.Select(t => new Setting
            {
                Key   = t.Key,
                Value = t.Value
            });

            BlogContext.AddRange(settingList);
        }
Exemplo n.º 7
0
            public void Insert_with_explicit_default_keys()
            {
                using (var context = new BlogContext())
                {
                    context.AddRange(new Blog {
                        Id = 0, Name = "One Unicorn"
                    }, new Blog {
                        Id = 1, Name = "Two Unicorns"
                    });

                    // DbUpdateException : An error occurred while updating the entries. See the
                    // inner exception for details.
                    // SqlException : Cannot insert explicit value for identity column in table
                    // 'Blog' when IDENTITY_INSERT is set to OFF.
                    Assert.Throws <DbUpdateException>(() => context.SaveChanges());
                }
            }
Exemplo n.º 8
0
            public void Insert_explicit_value_throws_when_readonly_before_save()
            {
                using (var context = new BlogContext())
                {
                    context.AddRange(new Blog {
                        Id = 1, Name = "One Unicorn"
                    }, new Blog {
                        Name = "Two Unicorns"
                    });

                    // The property 'Id' on entity type 'Blog' is defined to be read-only before it is
                    // saved, but its value has been set to something other than a temporary or default value.
                    Assert.Equal(
                        CoreStrings.PropertyReadOnlyBeforeSave("Id", "Blog"),
                        Assert.Throws <InvalidOperationException>(() => context.SaveChanges()).Message);
                }
            }
Exemplo n.º 9
0
            public void Insert_with_non_key_default_value()
            {
                using (var context = new BlogContext())
                {
                    context.Database.EnsureClean();

                    var blogs = new List <Blog>
                    {
                        new Blog {
                            Name = "One Unicorn"
                        },
                        new Blog {
                            Name = "Two Unicorns", CreatedOn = new DateTime(1969, 8, 3, 0, 10, 0)
                        }
                    };

                    context.AddRange(blogs);

                    context.SaveChanges();

                    Assert.NotEqual(new DateTime(), blogs[0].CreatedOn);
                    Assert.NotEqual(new DateTime(), blogs[1].CreatedOn);
                }

                using (var context = new BlogContext())
                {
                    var blogs = context.Blogs.OrderBy(e => e.Name).ToList();

                    Assert.NotEqual(new DateTime(), blogs[0].CreatedOn);
                    Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn);

                    blogs[0].CreatedOn = new DateTime(1973, 9, 3, 0, 10, 0);
                    blogs[1].Name      = "Zwo Unicorns";

                    context.SaveChanges();
                }

                using (var context = new BlogContext())
                {
                    var blogs = context.Blogs.OrderBy(e => e.Name).ToList();

                    Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn);
                    Assert.Equal(new DateTime(1973, 9, 3, 0, 10, 0), blogs[0].CreatedOn);
                }
            }
Exemplo n.º 10
0
            public void Insert_with_explicit_sentinel_keys()
            {
                using (var context = new BlogContext())
                {
                    context.AddRange(new Blog {
                        Id = 0, Name = "One Unicorn"
                    }, new Blog {
                        Id = 1, Name = "Two Unicorns"
                    });

                    // The property 'Id' on entity type 'Blog' has a temporary value while attempting to change
                    // the entity's state to 'Unchanged'. Either set a permanent value explicitly or ensure
                    // that the database is configured to generate values for this property.
                    Assert.Equal(
                        Internal.Strings.TempValuePersists("Id", "Blog", "Unchanged"),
                        Assert.Throws <InvalidOperationException>(() => context.SaveChanges()).Message);
                }
            }
Exemplo n.º 11
0
            public void Insert_with_non_key_default_value()
            {
                using (var context = new BlogContext())
                {
                    context.Database.EnsureClean();

                    context.AddRange(
                        new Blog {
                        Name = "One Unicorn"
                    },
                        new Blog {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();

                    Assert.NotEqual(new DateTime(), context.Blogs.ToList()[0].CreatedOn);
                }

                DateTime dateTime0;

                using (var context = new BlogContext())
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    dateTime0 = blogs[0].CreatedOn;

                    Assert.NotEqual(new DateTime(), dateTime0);
                    Assert.NotEqual(new DateTime(), blogs[1].CreatedOn);

                    blogs[0].Name      = "One Pegasus";
                    blogs[1].CreatedOn = new DateTime(1973, 9, 3, 0, 10, 0);

                    context.SaveChanges();
                }

                using (var context = new BlogContext())
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(dateTime0, blogs[0].CreatedOn);
                    Assert.Equal(new DateTime(1973, 9, 3, 0, 10, 0), blogs[1].CreatedOn);
                }
            }
Exemplo n.º 12
0
            public void Insert_with_Identity_column()
            {
                using (var context = new BlogContext())
                {
                    context.AddRange(new Blog {
                        Name = "One Unicorn"
                    }, new Blog {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContext())
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(1, blogs[0].Id);
                    Assert.Equal(2, blogs[1].Id);
                }
            }
Exemplo n.º 13
0
            public void Insert_with_default_value_from_sequence()
            {
                using (var context = new BlogContext())
                {
                    context.AddRange(new Blog {
                        Name = "One Unicorn"
                    }, new Blog {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContext())
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(77, blogs[0].Id);
                    Assert.Equal(78, blogs[1].Id);
                }
            }
Exemplo n.º 14
0
            public void Insert_with_explicit_non_default_keys()
            {
                using (var context = new BlogContext())
                {
                    context.AddRange(new Blog {
                        Id = 66, Name = "One Unicorn"
                    }, new Blog {
                        Id = 67, Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContext())
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(66, blogs[0].Id);
                    Assert.Equal(67, blogs[1].Id);
                }
            }
Exemplo n.º 15
0
            public void Insert_with_explicit_with_sentinel_keys()
            {
                using (var context = new BlogContext())
                {
                    context.AddRange(new Blog {
                        Id = 0, Name = "One Unicorn"
                    }, new Blog {
                        Id = 1, Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContext())
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(0, blogs[0].Id);
                    Assert.Equal(1, blogs[1].Id);
                }
            }
Exemplo n.º 16
0
            public void Insert_with_sequence_HiLo()
            {
                using (var context = new BlogContext())
                {
                    context.Database.EnsureClean();

                    context.AddRange(new Blog {
                        Name = "One Unicorn"
                    }, new Blog {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContext())
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(1, blogs[0].Id);
                    Assert.Equal(2, blogs[1].Id);
                }
            }
        public void Insert_with_explicit_non_default_keys_by_default()
        {
            using (var testStore = SqlServerTestStore.Create(DatabaseName))
            {
                using (var context = new BlogContext(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    context.AddRange(new Blog {
                        Id = 1, Name = "One Unicorn"
                    }, new Blog {
                        Id = 2, Name = "Two Unicorns"
                    });

                    // DbUpdateException : An error occurred while updating the entries. See the
                    // inner exception for details.
                    // SqlException : Cannot insert explicit value for identity column in table
                    // 'Blog' when IDENTITY_INSERT is set to OFF.
                    context.Database.CreateExecutionStrategy().Execute(c =>
                                                                       Assert.Throws <DbUpdateException>(() => c.SaveChanges()), context);
                }
            }
        }
 public List <EmployeeEntity> Add(List <EmployeeEntity> employees)
 {
     _context.AddRange(employees);
     _context.SaveChanges();
     return(employees);
 }
Exemplo n.º 19
0
 public List <DepartmentEntity> Add(List <DepartmentEntity> departments)
 {
     _context.AddRange(departments);
     _context.SaveChanges();
     return(departments);
 }