本文整理汇总了C#中DataContext.All方法的典型用法代码示例。如果您正苦于以下问题:C# DataContext.All方法的具体用法?C# DataContext.All怎么用?C# DataContext.All使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataContext
的用法示例。
在下文中一共展示了DataContext.All方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public void Execute()
{
var manufacturerId = 0;
BeginTest.To(new DataContext())
.Should("Get datacontext transaction mode").Assert(d => d.TransactionMode == Transaction.Begin)
.Should("Get a value from database").Assert(d =>
{
manufacturerId = d.GetValue<int>("SELECT TOP 1 TheId FROM Le_Manufacturer");
return manufacturerId > 0;
})
.Should("Insert car with a transactional datacontext").Assert(d =>
{
var car = new Car { Name = "Esprit", ModelYear = 1976, ManufacturerId = manufacturerId };
var id = d.Execute("INSERT INTO Car VALUES (@Name, @ModelYear, @Date, @Chassis, @Mileage, @ManufacturerId)", car);
return id > 0;
})
.Should("Select new car without commit datacontext").Assert(d =>
{
var car = d.First<Car>("SELECT * FROM Car WHERE Mileage IS NULL");
return car != null;
})
.Should("Commit datacontext transaction").Assert(d =>
{
d.Commit();
return true;
})
.Should("Select commited data on database with another datacontext").Assert(d =>
{
using (var dataContext = new DataContext())
{
var cars = dataContext.All<Car>("SELECT * FROM Car");
return cars.Count == 1;
}
})
.Should("Get values from database").Assert(d =>
{
var mileages = d.GetValues<double?>("SELECT Mileage FROM Car");
return mileages.Count == 1 && mileages[0] == null;
})
.Should("Throw error when property format exception").Assert(d =>
{
try
{
var car = d.First<ErrorCar>("SELECT * FROM Car");
}
catch (FormatException) { return true; }
return false;
})
.Should("Dispose datacontext close connection and transaction").Assert(d =>
{
d.Dispose();
return d.Provider.DbConnection.State == ConnectionState.Closed;
});
}
示例2: K_DataContext_Reader_All_Should_Return_An_Empty_Object_List_When_Query_Do_Not_Found_Items
public void K_DataContext_Reader_All_Should_Return_An_Empty_Object_List_When_Query_Do_Not_Found_Items()
{
using (var context = new DataContext())
{
var cars = context.All<Car>("SELECT * FROM Car WHERE Name = 'Fusca'");
cars.Should().BeEmpty();
}
}
示例3: D_DataContext_Transitional_Should_Not_Commit_Data_If_Not_Explicit_Commit_Command_Call
public void D_DataContext_Transitional_Should_Not_Commit_Data_If_Not_Explicit_Commit_Command_Call()
{
using (var context = new DataContext())
{
var insertParameters = new { Name = "Bentley", Year = 1919 };
context.Execute("INSERT INTO Le_Manufacturer VALUES (@Name, @Year)", insertParameters);
}
using (var context = new DataContext())
{
var manufacturers = context.All<Manufacturer>("SELECT * FROM Le_Manufacturer");
manufacturers.Count().Should().Be(3);
manufacturers.Should().Contain(m => m.Name == "Lotus");
manufacturers.Should().Contain(m => m.Name == "General Motors");
}
}
示例4: G_DataContext_Should_Execute_A_Procedure
public void G_DataContext_Should_Execute_A_Procedure()
{
using (var context = new DataContext())
{
var procedureParams = new { Status = "active" };
var whoResults = context.All<WhoResult>("EXEC sp_who @Status", procedureParams);
whoResults.Should().Contain(r => r.dbname == "ThunderTest" && r.cmd == "SELECT");
}
}
示例5: DataContext_Should_Get_A_List_Of_Objects_From_Database
public void DataContext_Should_Get_A_List_Of_Objects_From_Database()
{
using (var context = new DataContext())
{
var cars = context.All<object>("SELECT * FROM Airplane");
}
connectionMock.Verify(c => c.Open(), Times.Once());
connectionMock.Verify(c => c.Close(), Times.Once());
connectionMock.Verify(c => c.BeginTransaction(), Times.Never());
commandMock.Verify(c => c.ExecuteReader(), Times.Once());
commandMock.VerifySet(c => c.CommandText = "SELECT * FROM Airplane");
providerMock.Object.DbTransaction.Should().BeNull();
dataReaderMock.Verify(r => r.Read(), Times.Once());
}