当前位置: 首页>>代码示例>>C#>>正文


C# SimpleModelContext.SaveChanges方法代码示例

本文整理汇总了C#中SimpleModel.SimpleModelContext.SaveChanges方法的典型用法代码示例。如果您正苦于以下问题:C# SimpleModelContext.SaveChanges方法的具体用法?C# SimpleModelContext.SaveChanges怎么用?C# SimpleModelContext.SaveChanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SimpleModel.SimpleModelContext的用法示例。


在下文中一共展示了SimpleModelContext.SaveChanges方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Explicit_rollback_can_be_used_to_rollback_a_transaction

        public void Explicit_rollback_can_be_used_to_rollback_a_transaction()
        {
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            using (var tx = new TransactionScope())
            {
                using (var context = new SimpleModelContext())
                {
                    var product = new Product
                                      {
                                          Name = "BestTea"
                                      };
                    context.Products.Add(product);
                    context.SaveChanges();

                    Assert.Equal(1, GetTransactionCount(context.Database.Connection));
                    Assert.True(context.Products.Where(p => p.Name == "BestTea").AsNoTracking().Any());

                    // Rollback System Transaction
                    tx.Dispose();

                    Assert.False(context.Products.Where(p => p.Name == "BestTea").AsNoTracking().Any());
                }
            }
        }
开发者ID:jesusico83,项目名称:Telerik,代码行数:25,代码来源:TransactionTests.cs

示例2: Insert_In_SystemTransaction_without_commit_works_and_transaction_count_is_correct

        public void Insert_In_SystemTransaction_without_commit_works_and_transaction_count_is_correct()
        {
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            using (new TransactionScope())
            {
                using (var context = new SimpleModelContext())
                {
                    var product = new Product { Name = "Fanta" };
                    context.Products.Add(product);
                    context.SaveChanges();

                    Assert.Equal(1, GetTransactionCount(context.Database.Connection));
                    Assert.True(context.Products.Where(p => p.Name == "Fanta").AsNoTracking().Any());
                }
            }
        }
开发者ID:jimmy00784,项目名称:entityframework,代码行数:17,代码来源:TransactionTests.cs

示例3: Explicit_rollback_on_a_local_transaction_can_be_used_to_rollback_a_transaction

        public void Explicit_rollback_on_a_local_transaction_can_be_used_to_rollback_a_transaction()
        {
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            using (var context = new SimpleModelContext())
            {
                // Begin a local transaction
                var transaction = BeginLocalTransaction(context);

                var product = new Product() { Name = "New Tea" };
                context.Products.Add(product);
                context.SaveChanges();

                Assert.True(context.Products.Where(p => p.Name == "New Tea").AsNoTracking().Any());

                // Rollback local transaction
                transaction.Rollback();
                CloseEntityConnection(context);

                Assert.False(context.Products.Where(p => p.Name == "New Tea").AsNoTracking().Any());
            }
        }
开发者ID:jimmy00784,项目名称:entityframework,代码行数:22,代码来源:TransactionTests.cs

示例4: SaveChanges_bubbles_exception_during_AcceptChanges_implementation

        private void SaveChanges_bubbles_exception_during_AcceptChanges_implementation(Func<DbContext, int> saveChanges)
        {
            using (var context = new SimpleModelContext())
            {
                var cat1 = new Category { Id = "AUSSIE FOODS" };
                var cat2 = new Category { Id = "AUSSIE FOODS" };

                context.Categories.Attach(cat1);
                context.Categories.Add(cat2);

                // Accept will fail because of PK violation
                // (cat1 doesn't actually exist in the store so update pipeline will succeed)
                Assert.Throws<InvalidOperationException>(() => context.SaveChanges()).ValidateMessage(
                    "ObjectContext_AcceptAllChangesFailure");
            }
        }
开发者ID:jimmy00784,项目名称:entityframework,代码行数:16,代码来源:DbContextTests.cs

示例5: SaveChanges_bubbles_UpdateException_implementation

        private void SaveChanges_bubbles_UpdateException_implementation(Func<DbContext, int> saveChanges)
        {
            using (var context = new SimpleModelContext())
            {
                var prod = new Product() { Name = "Wallaby Sausages", CategoryId = "AUSSIE FOODS" };
                context.Products.Add(prod);

                Assert.Throws<DbUpdateException>(() => context.SaveChanges()).ValidateMessage(
                    "Update_GeneralExecutionException");
            }
        }
开发者ID:jimmy00784,项目名称:entityframework,代码行数:11,代码来源:DbContextTests.cs

示例6: SaveChanges_on_uninitialized_context_does_not_throw

 public void SaveChanges_on_uninitialized_context_does_not_throw()
 {
     using (var context = new SimpleModelContext())
     {
         Assert.Equal(0, context.SaveChanges());
     }
 }
开发者ID:jimmy00784,项目名称:entityframework,代码行数:7,代码来源:DbContextTests.cs

示例7: Scenario_Using_two_databases

        public void Scenario_Using_two_databases()
        {
            EnsureDatabaseInitialized(() => new LoginsContext());
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            using (new TransactionScope())
            {
                using (var context = new LoginsContext())
                {
                    var login = new Login() { Id = Guid.NewGuid(), Username = "elmo" };
                    context.Logins.Add(login);
                    context.SaveChanges();

                    // Scenario ends; simple validation of final state follows
                    Assert.Same(login, context.Logins.Find(login.Id));
                    Assert.Equal(EntityState.Unchanged, GetStateEntry(context, login).State);
                }
            }

            using (new TransactionScope())
            {
                using (var context = new SimpleModelContext())
                {
                    var category = new Category() { Id = "Books" };
                    var product = new Product() { Name = "The Unbearable Lightness of Being", Category = category };
                    context.Products.Add(product);
                    context.SaveChanges();

                    // Scenario ends; simple validation of final state follows
                    Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State);
                    Assert.Equal(EntityState.Unchanged, GetStateEntry(context, category).State);
                    Assert.Equal("Books", product.CategoryId);
                    Assert.Same(category, product.Category);
                    Assert.True(category.Products.Contains(product));
                }
            }
        }
开发者ID:WangWilliam,项目名称:EntityFramework5,代码行数:37,代码来源:SimpleScenarios.cs

示例8: Scenario_Relate_using_FK

        public void Scenario_Relate_using_FK()
        {
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            using (new TransactionScope())
            {
                using (var context = new SimpleModelContext())
                {
                    var product = new Product() { Name = "Bovril", CategoryId = "Foods" };
                    context.Products.Add(product);
                    context.SaveChanges();

                    // Scenario ends; simple validation of final state follows
                    Assert.NotNull(product);
                    Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State);
                    Assert.Equal("Foods", product.CategoryId);
                }
            }
        }
开发者ID:WangWilliam,项目名称:EntityFramework5,代码行数:19,代码来源:SimpleScenarios.cs

示例9: Scenario_Update

        public void Scenario_Update()
        {
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            using (new TransactionScope())
            {
                using (var context = new SimpleModelContext())
                {
                    var product = context.Products.Find(1);
                    product.Name = "iSnack 2.0";
                    context.SaveChanges();

                    // Scenario ends; simple validation of final state follows
                    Assert.Equal("iSnack 2.0", product.Name);
                    Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State);
                }
            }
        }
开发者ID:WangWilliam,项目名称:EntityFramework5,代码行数:18,代码来源:SimpleScenarios.cs

示例10: Scenario_Insert

        public void Scenario_Insert()
        {
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            using (new TransactionScope())
            {
                using (var context = new SimpleModelContext())
                {
                    var product = new Product() { Name = "Vegemite" };
                    context.Products.Add(product);
                    context.SaveChanges();

                    // Scenario ends; simple validation of final state follows
                    Assert.NotEqual(0, product.Id);
                    Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State);
                }
            }
        }
开发者ID:WangWilliam,项目名称:EntityFramework5,代码行数:18,代码来源:SimpleScenarios.cs

示例11: Scenario_Relate_using_query

        public void Scenario_Relate_using_query()
        {
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                () =>
                {
                    using (new TransactionScope())
                    {
                        using (var context = new SimpleModelContext())
                        {
                            var category = context.Categories.Find("Foods");
                            var product = new Product
                            {
                                Name = "Bovril",
                                Category = category
                            };
                            context.Products.Add(product);
                            context.SaveChanges();

                            // Scenario ends; simple validation of final state follows
                            Assert.NotNull(product);
                            Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State);

                            Assert.NotNull(category);
                            Assert.Equal(EntityState.Unchanged, GetStateEntry(context, category).State);

                            Assert.Equal("Foods", product.CategoryId);
                            Assert.Same(category, product.Category);
                            Assert.True(category.Products.Contains(product));
                        }
                    }
                });
        }
开发者ID:Cireson,项目名称:EntityFramework6,代码行数:34,代码来源:SimpleScenarios.cs


注:本文中的SimpleModel.SimpleModelContext.SaveChanges方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。