本文整理汇总了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());
}
}
}
示例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());
}
}
}
示例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());
}
}
示例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");
}
}
示例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");
}
}
示例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());
}
}
示例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));
}
}
}
示例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);
}
}
}
示例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);
}
}
}
示例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);
}
}
}
示例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));
}
}
});
}