本文整理汇总了C#中DataContext.Commit方法的典型用法代码示例。如果您正苦于以下问题:C# DataContext.Commit方法的具体用法?C# DataContext.Commit怎么用?C# DataContext.Commit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataContext
的用法示例。
在下文中一共展示了DataContext.Commit方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldLogAtDebugLevel
public void ShouldLogAtDebugLevel()
{
//arrange
var logger = new ConsoleOutLogger("Testing", LogLevel.Trace, true, true, true, string.Empty);
var target = new DataContext(Settings.Default.Connection, new DriversEducationMappings(), logger);
//act
var firstDriver = new Driver("Devlin", "Liles");
target.Add(firstDriver);
target.Add(new Driver("Tim", "Rayburn"));
target.Add(new Driver("Jay", "Smith"));
target.Add(new Driver("Brian", "Sullivan"));
target.Add(new Driver("Cori", "Drew"));
target.Commit();
target.Reload(firstDriver);
foreach (var driver in target.AsQueryable<Driver>())
{
target.Remove(driver);
}
target.Commit();
target.ExecuteSqlQuery<Driver>("Select * from Drivers Where LastName = @lastName",
new DbParameter[] {new SqlParameter("lastName", "Liles")});
//assert
//Assert.Inconclusive("We fail here to get the output from console nice and easy");
}
示例2: DataObjectQuery_Should_Select_All_Items_From_Database_With_Parameter_Binding_And_Context_Transaction
public void DataObjectQuery_Should_Select_All_Items_From_Database_With_Parameter_Binding_And_Context_Transaction()
{
commandMock.Setup(c => c.CommandText).Returns("SELECT [Id], [Name], [FirstFlight] FROM Airplane WHERE Name = @Name");
using (var context = new DataContext())
{
// Execute a command to open transaction.
context.Execute("DELETE FROM Airplane");
// Simulate connection open state later the first execute.
connectionMock.Setup(c => c.State).Returns(ConnectionState.Open);
select.With(context).All("WHERE Name = @Name", new { Name = "Omega" });
context.Commit();
}
connectionMock.Verify(c => c.CreateCommand(), Times.Exactly(2));
connectionMock.Verify(c => c.Open(), Times.Once());
connectionMock.Verify(c => c.BeginTransaction(), Times.Once());
connectionMock.Verify(c => c.Close(), Times.Once());
commandMock.Verify(c => c.ExecuteReader(), Times.Once());
commandMock.VerifySet(c => c.CommandText = "SELECT [Id], [Name], [FirstFlight] FROM Airplane WHERE Name = @Name");
commandMock.VerifySet(c => c.Connection = connectionMock.Object);
commandMock.VerifySet(c => c.Transaction = transactionMock.Object);
commandMock.Verify(c => c.CreateParameter(), Times.Once());
parameterMock.VerifySet(p => p.ParameterName = "@Name");
parameterMock.VerifySet(p => p.Value = "Omega");
parameterCollectionMock.Verify(p => p.Add(parameterMock.Object), Times.Once());
transactionMock.Verify(t => t.Commit(), Times.Once());
}
示例3: CreateTables
private void CreateTables()
{
var createManufacturerTable = new StringBuilder("CREATE TABLE Le_Manufacturer (")
.Append("[TheId] NUMERIC(8) NOT NULL IDENTITY PRIMARY KEY,")
.Append("[Name] VARCHAR(32) NOT NULL,")
.Append("[BuildYear] NUMERIC(4) NOT NULL)")
.ToString();
var createCarTable = new StringBuilder("CREATE TABLE Car (")
.Append("[Id] NUMERIC(8) NOT NULL IDENTITY PRIMARY KEY,")
.Append("[Name] VARCHAR(32) NOT NULL,")
.Append("[ModelYear] NUMERIC(4) NOT NULL,")
.Append("[Date] DATETIME NOT NULL,")
.Append("[Chassis] VARCHAR(32) NOT NULL,")
.Append("[Mileage] FLOAT,")
.Append("[ManufacturerId] NUMERIC(8) REFERENCES Le_Manufacturer(TheId))")
.ToString();
using (var data = new DataContext())
{
data.Execute(createManufacturerTable);
data.Execute(createCarTable);
data.Commit();
}
}
示例4: DataContext_Should_Execute_And_Commit_Some_SQL_Commands_With_Transaction
public void DataContext_Should_Execute_And_Commit_Some_SQL_Commands_With_Transaction()
{
using (var context = new DataContext())
{
context.Execute("DELETE FROM Airplane");
// Simulate connection open state later the first execute.
connectionMock.Setup(c => c.State).Returns(ConnectionState.Open);
context.Execute("DELETE FROM Tools");
context.Commit();
}
connectionMock.Verify(c => c.CreateCommand(), Times.Exactly(2));
connectionMock.Verify(c => c.Open(), Times.Once());
connectionMock.Verify(c => c.BeginTransaction(), Times.Once());
connectionMock.Verify(c => c.Close(), Times.Once());
commandMock.Verify(c => c.ExecuteNonQuery(), Times.Exactly(2));
commandMock.VerifySet(c => c.CommandText = "DELETE FROM Airplane");
commandMock.VerifySet(c => c.CommandText = "DELETE FROM Tools");
commandMock.VerifySet(c => c.Connection = connectionMock.Object);
commandMock.VerifySet(c => c.Transaction = transactionMock.Object);
commandMock.Verify(c => c.CreateParameter(), Times.Never());
transactionMock.Verify(t => t.Commit(), Times.Once());
providerMock.Object.DbTransaction.Should().NotBeNull();
}
示例5: using
public void B_DataContext_Transitional_Should_Execute_A_Insert_SQL_Command_With_Array_Parameters_And_Read_Created_Data_In_Same_And_Count_Data_In_Another_Context()
{
using (var context = new DataContext())
{
context.Execute("INSERT INTO Le_Manufacturer VALUES (@0, @1)", "McLaren", 1963);
var manufacturer = context.First<Manufacturer>("SELECT TOP 1 * FROM Le_Manufacturer WHERE Name = @0", "McLaren");
context.Commit();
manufacturer.TheId.Should().BeGreaterThan(0);
manufacturer.Name.Should().Be("McLaren");
manufacturer.BuildYear.Should().Be(1963);
}
using (var context = new DataContext())
{
var manufacturerCount = context.GetValue<int>("SELECT COUNT(TheId) FROM Le_Manufacturer");
manufacturerCount.Should().Be(2);
}
}
示例6: DataContext_Should_Execute_And_Commit_A_SQL_Command_With_Transaction
public void DataContext_Should_Execute_And_Commit_A_SQL_Command_With_Transaction()
{
using (var context = new DataContext())
{
context.Execute("DELETE FROM Airplane");
context.Commit();
}
connectionMock.Verify(c => c.CreateCommand(), Times.Once());
connectionMock.Verify(c => c.Open(), Times.Once());
connectionMock.Verify(c => c.BeginTransaction(), Times.Once());
connectionMock.Verify(c => c.Close(), Times.Once());
commandMock.Verify(c => c.ExecuteNonQuery(), Times.Once());
commandMock.VerifySet(c => c.CommandText = "DELETE FROM Airplane");
commandMock.VerifySet(c => c.Connection = connectionMock.Object);
commandMock.VerifySet(c => c.Transaction = transactionMock.Object);
commandMock.Verify(c => c.CreateParameter(), Times.Never());
transactionMock.Verify(t => t.Commit(), Times.Once());
providerMock.Object.DbTransaction.Should().NotBeNull();
}
示例7: DataObjectCommand_Should_Receive_A_Context_To_Make_Transactional_Commands
public void DataObjectCommand_Should_Receive_A_Context_To_Make_Transactional_Commands()
{
using (var context = new DataContext())
{
command.Insert(target, context);
// Simulate connection open state later the first execute.
connectionMock.Setup(c => c.State).Returns(ConnectionState.Open);
command.Insert(target, context);
context.Commit();
}
connectionMock.Verify(c => c.CreateCommand(), Times.Exactly(2));
connectionMock.Verify(c => c.Open(), Times.Once());
connectionMock.Verify(c => c.BeginTransaction(), Times.Once());
connectionMock.Verify(c => c.Close(), Times.Once());
commandMock.Verify(c => c.ExecuteScalar(), Times.Exactly(2));
commandMock.VerifySet(c => c.Connection = connectionMock.Object);
transactionMock.Verify(t => t.Commit(), Times.Once());
}
示例8: S_DataObject_Should_CRUD_An_Item_With_Shared_Transactional_Data_Context
public void S_DataObject_Should_CRUD_An_Item_With_Shared_Transactional_Data_Context()
{
using (var context = new DataContext())
{
var dodge = new Manufacturer { Name = "Dodge", BuildYear = 1900 };
manufacturerDataObject.Insert(dodge, context);
dodge.BuildYear = 1915;
manufacturerDataObject.Update(dodge, context);
var updatedDodge = context.First<Manufacturer>("SELECT TOP 1 * FROM Le_Manufacturer WHERE Name = 'Dodge'");
var m4s = new Car { Name = "M4S", ModelYear = 1984, Category = CarCategory.Prototype, ManufacturerId = dodge.TheId };
carDataObject.Insert(m4s, context);
var createdM4s = carDataObject.Select.With(context).First("WHERE Name = 'M4S'");
carDataObject.Delete(m4s, context);
var deletedM4s = carDataObject.Select.With(context).First("WHERE Name = 'M4S'");
updatedDodge.BuildYear.Should().Be(1915);
createdM4s.Should().NotBeNull();
deletedM4s.Should().BeNull();
context.Commit();
}
var createdDodgeOutsiteContext = manufacturerDataObject.Select.First("WHERE Name = 'Dodge'");
createdDodgeOutsiteContext.Should().NotBeNull();
}
示例9: F_DataContext_Should_Read_Data_With_One_Field_And_Insert_A_Data_With_Some_Parameters_Types
public void F_DataContext_Should_Read_Data_With_One_Field_And_Insert_A_Data_With_Some_Parameters_Types()
{
using (var context = new DataContext())
{
var selectParameters = new { Name = "Lotus" };
var manufacturer = context.First<Manufacturer>("SELECT TheId FROM Le_Manufacturer WHERE Name = @Name", selectParameters);
var insertParameters = new Car
{
Name = "Esprit Turbo",
ModelYear = 1981,
Mileage = 318.19850801,
ManufacturerId = manufacturer.TheId,
Category = CarCategory.Sport
};
context.Execute("INSERT INTO Car VALUES (@Name, @ModelYear, @CreatedAt, @Chassis, @Mileage, @Category, @ManufacturerId)", insertParameters);
var car = context.First<Car>("SELECT TOP 1 * FROM Car");
context.Commit();
car.Id.Should().BeGreaterThan(0);
car.Name.Should().Be("Esprit Turbo");
car.ModelYear.Should().Be(1981);
car.Mileage.Should().Be(318.19850801);
car.ManufacturerId.Should().Be(manufacturer.TheId);
car.CreatedAt.Date.Should().Be(DateTime.Today.Date);
car.Category.Should().Be(CarCategory.Sport);
}
}
示例10: RegisterModified_WithModifiedPost_PersistsPost
public void RegisterModified_WithModifiedPost_PersistsPost(
string databaseName,
Post post,
string modifiedValue)
{
// Given
var sut = new DataContext(databaseName);
sut.RegisterAdded(post);
sut.Commit();
// When
post.Title = modifiedValue;
sut.RegisterModified(post);
sut.Commit();
var result = sut.Get<Post>().FirstOrDefault();
// Then
Assert.Equal(post, result);
sut.Database.Delete();
sut.Dispose();
}
示例11: RegisterDeleted_WithPost_RemovesPost
public void RegisterDeleted_WithPost_RemovesPost(
string databaseName,
Post post)
{
// Given
var sut = new DataContext(databaseName);
sut.RegisterAdded(post);
sut.Commit();
// When
sut.RegisterDeleted(post);
sut.Commit();
var result = sut.Get<Post>().Any();
// Then
Assert.False(result);
sut.Database.Delete();
sut.Dispose();
}