存储过程
使用SQL语句:
1: var rowsAffected = Context.Sql("ProductUpdate")
2: .CommandType(DbCommandTypes.StoredProcedure)
3: .Parameter("ProductId", 1)
4: .Parameter("Name", "The Warren Buffet Way")
5: .Execute();
使用builder:
1: var rowsAffected = Context.StoredProcedure("ProductUpdate")
2: .Parameter("Name", "The Warren Buffet Way")
3: .Parameter("ProductId", 1).Execute();
使用builder,并且自动映射
1: var product = Context.Sql("select * from Product where ProductId = 1")
2: .QuerySingle<Product>();
3:
4: product.Name = "The Warren Buffet Way";
5:
6: var rowsAffected = Context.StoredProcedure<Product>("ProductUpdate", product)
7: .AutoMap(x => x.CategoryId).Execute();
使用Lambda表达式
1: var product = Context.Sql("select * from Product where ProductId = 1")
2: .QuerySingle<Product>();
3: product.Name = "The Warren Buffet Way";
4:
5: var rowsAffected = Context.StoredProcedure<Product>("ProductUpdate", product)
6: .Parameter(x => x.ProductId)
7: .Parameter(x => x.Name).Execute();
事务
FluentData 支持事务。如果使用事务,最好使用using语句将代码包起来,已保证连接会被关闭。默认的,如果查询过程发生异常,如事务不会被提交,会进行回滚。
1: using (var context = Context.UseTransaction(true))
2: {
3: context.Sql("update Product set Name = @0 where ProductId = @1")
4: .Parameters("The Warren Buffet Way", 1)
5: .Execute();
6:
7: context.Sql("update Product set Name = @0 where ProductId = @1")
8: .Parameters("Bill Gates Bio", 2)
9: .Execute();
10:
11: context.Commit();
12: }
13:
实体工厂
实体工厂负责在自动映射的时候,生成POCO实例。如果需要生成复杂的实例,可以自定义实体工厂:
1: List<Product> products = Context.EntityFactory(new CustomEntityFactory())
2: .Sql("select * from Product")
3: .QueryMany<Product>();
4:
5: public class CustomEntityFactory : IEntityFactory
6: {
7: public virtual object Resolve(Type type)
8: {
9: return Activator.CreateInstance(type);
10: }
11: }
Stored procedure
Using SQL:
var rowsAffected = Context.Sql("ProductUpdate")
.CommandType(DbCommandTypes.StoredProcedure)
.Parameter("ProductId", 1)
.Parameter("Name", "The Warren Buffet Way")
.Execute();
Using a builder:
var rowsAffected = Context.StoredProcedure("ProductUpdate")
.Parameter("Name", "The Warren Buffet Way")
.Parameter("ProductId", 1).Execute();
Using a builder with automapping:
var product = Context.Sql("select * from Product where ProductId = 1")
.QuerySingle<Product>();
product.Name = "The Warren Buffet Way";
var rowsAffected = Context.StoredProcedure<Product>("ProductUpdate", product)
.AutoMap(x => x.CategoryId).Execute();
Using a builder with automapping and expressions:
var product = Context.Sql("select * from Product where ProductId = 1")
.QuerySingle<Product>();
product.Name = "The Warren Buffet Way";
var rowsAffected = Context.StoredProcedure<Product>("ProductUpdate", product)
.Parameter(x => x.ProductId)
.Parameter(x => x.Name).Execute();
Transactions
FluentData supports transactions. When you use transactions its important to wrap the code inside a using statement to make sure that the database connection is closed. By default, if any exception occur or if Commit is not called then Rollback will automatically be called.
using (var context = Context.UseTransaction(true))
{
context.Sql("update Product set Name = @0 where ProductId = @1")
.Parameters("The Warren Buffet Way", 1)
.Execute();
context.Sql("update Product set Name = @0 where ProductId = @1")
.Parameters("Bill Gates Bio", 2)
.Execute();
context.Commit();
}
Entity factory
The entity factory is responsible for creating object instances during automapping. If you have some complex business objects that require special actions during creation, you can create your own custom entity factory:
List<Product> products = Context.EntityFactory(new CustomEntityFactory())
.Sql("select * from Product")
.QueryMany<Product>();
public class CustomEntityFactory : IEntityFactory
{
public virtual object Resolve(Type type)
{
return Activator.CreateInstance(type);
}
}