FluentData入门
更新时间:2024-06-18 12:57:01 阅读量: 综合文库 文档下载
FluentData入门(一)--核心概念
DbContext类
这是FluentData的核心类,可以通过配置ConnectionString来定义这个类,如何连接数据库和对具体DbContext类
这是FluentData的核心类,可以通过配置ConnectionString来定义这个类,如何连接数据库和对具体的哪个数据库进行数据查询操作。
DbCommand类
这个类负责在相对应的数据库执行具体的每一个数据操作。
Events
DbContext类定义了以下这些事件: OnConnectionClosed OnConnectionOpened OnConnectionOpening OnError OnExecuted OnExecuting
可以在事件中,记录每个SQL查询错误或者SQL查询执行的 时间等信息。 Builders
Builder用来创建Insert, Update, Delete等相关的DbCommand实例。 Mapping
FluentData可以将SQL查询结果自动映射成一个POCO(POCO - Plain Old CLR Object)实体类,也可以转换成一个dynamic类型。
自动转成实体类:
如果字段名中不包含下划线(\,将映射到具有相同名字的属性上,例如:字段名 \将映射到属性名 \。
如果字段名中不包含下划线(\,将映射到内嵌的属性上,例如:字段名 \将映射
到属性名 \。
如果数据库字段名和属性名不一致,可以使用SQL的as让他们一致。
自动转换成dynamic类型:
对应dynamic类型,会为每个字段生成一个同名的属性,例如:字段名 \将映射到属性名 \。
什么时候应该主动释放资源?
如果使用UseTransaction或者UseSharedConnection,那么DbContext需要主动释放。 如果使用UseMultiResult或者MultiResultSql,那么DbCommand需要主动释放。 如果使用UseMultiResult,那么StoredProcedureBuilder需要主动释放。
其他所有的类都会自动释放,也就是说,一个数据库连接只在查询开始前才进行,查询结束后会马上关闭。的哪个数据库进行数据查询操作。
DbCommand类
这个类负责在相对应的数据库执行具体的每一个数据操作。
Events
DbContext类定义了以下这些事件:
? ? ? ? ? ?
OnConnectionClosed OnConnectionOpened OnConnectionOpening OnError OnExecuted OnExecuting
可以在事件中,记录每个SQL查询错误或者SQL查询执行的 时间等信息。 Builders
Builder用来创建Insert, Update, Delete等相关的DbCommand实例。 Mapping
FluentData可以将SQL查询结果自动映射成一个POCO(POCO - Plain Old CLR Object)实体类,也可以转换成一个dynamic类型。
自动转成实体类:
? 如果字段名中不包含下划线(\,将映射到具有相同名字的属性上,例如:字段名 \将映射到属性名 \。
? 如果字段名中不包含下划线(\,将映射到内嵌的属性上,例如:字段名 \将映射到属性名 \。
如果数据库字段名和属性名不一致,可以使用SQL的as让他们一致。
自动转换成dynamic类型:
? 对应dynamic类型,会为每个字段生成一个同名的属性,例如:字段名 \将映射到属性名 \。
? 什么时候应该主动释放资源?
如果使用UseTransaction或者UseSharedConnection,那么DbContext需要主动释放。
? 如果使用UseMultiResult或者MultiResultSql,那么DbCommand需要主动释放。 ? 如果使用UseMultiResult,那么StoredProcedureBuilder需要主动释放。
其他所有的类都会自动释放,也就是说,一个数据库连接只在查询开始前才进行,查询结束后会马上关闭。
FluentData入门(二)--创建DbContext
如何创建和初始化一个DbContext
可以在*.config文件中配置connection string,将connection string name或者将整个connection string 作为参数传递给DbContext来创建DbContext。
重要配置
IgnoreIfAutoMapFails – IDbContext.IgnoreIfAutoMapFails返回一个IDbContext,该实例中,如果自动映射失败时是否抛出异常
通过*.config中配置的ConnectionStringName创建一个DbContext
1: public IDbContext Context() 2: {
3: return new DbContext().ConnectionStringName(\, 4: new SqlServerProvider()); 5: }
调用DbContext的ConnectionString方法显示设置connection string来创建
1: public IDbContext Context() 2: {
3: return new DbContext().ConnectionString(
4: \, new SqlServerProvider()); 5: }
其他可以使用的Provider
只有通过使用不同的Provider,就可以切换到不同类型的数据库服务器上,比如
AccessProvider, DB2Provider, OracleProvider, MySqlProvider, PostgreSqlProvider, SqliteProvider, SqlServerCompact, SqlAzureProvider, SqlServerProvider.
FluentData入门(三)--Query 查询一组数据
返回一组dynamic对象(new in .NET 4.0)
1: List
返回一组强类型对象
1: List
返回一个自定义的Collection
1: ProductionCollection products = Context.Sql(\ProductionCollection>();
返回单个对象 返回一个dynamic对象
1: dynamic product = Context.Sql(@\
返回一个强类型对象:
1: Product product = Context.Sql(@\
返回一个DataTable:
1: DataTable products = Context.Sql(\
其实QueryMany
1: int numberOfProducts = Context.Sql(@\
返回一组标量值
1: List
设置查询参数: 索引顺序形式的参数
1: dynamic products = Context.Sql(@\2).QueryMany
或者
1: dynamic products = Context.Sql(@\@1\
名字形式的参数:
1: var command = Context.Sql(@\ 2: where ProductId=1\
3: .ParameterOut(\, DataTypes.String, 100); 4: command.Execute(); 5:
6: string productName = command.ParameterValue
8: List of parameters - in operator:
9: List
11: dynamic products = Context.Sql(@\ 12: where ProductId in(@0)\
Output 参数:
1: dynamic products = Context.Sql(@\
2: where ProductId = @ProductId1 or ProductId = @ProductId2\ 3: .Parameter(\, 1) 4: .Parameter(\, 2) 5: .QueryMany
FluentData入门(四)--Mapping
映射
自动映射 – 在数据库对象和.Net object自动进行1:1匹配
1: List
2: from Product\
3: .QueryMany
自动映射到一个自定义的Collection:
1: ProductionCollection products = Context.Sql(\).QueryMany
如果数据库字段和POCO类属性名不一致,使用SQL别名语法AS:
1: List
2: c.CategoryId as Category_CategoryId,
3: c.Name as Category_Name
4: from Product p
5: inner join Category c on p.CategoryId = c.CategoryId\
6: .QueryMany
在这里p.*中的ProductId和ProductName会自动映射到Prodoct.ProductId和Product.ProductName,而
Category_CategoryId
和
Category_Name 将映射到 Product.Category.CategoryId 和
Product.Category.Name.
使用dynamic自定义映射规则
1: List
2: .QueryMany
3:
4: public void Custom_mapper_using_dynamic(Product product, dynamic row)
5: {
6: product.ProductId = row.ProductId;
7: product.Name = row.Name;
8: }
使用datareader进行自定义映射:
1: List
2: .QueryMany
3:
4: public void Custom_mapper_using_datareader(Product product, IDataReader row)
5: {
6: product.ProductId = row.GetInt32(\);
7: product.Name = row.GetString(\);
8: }
或者,当你需要映射到一个复合类型时,可以使用QueryComplexMany或者QueryComplexSingle。
1: var products = new List
2: Context.Sql(\).QueryComplexMany
3:
4: private void MapComplexProduct(IList
5: {
6: var product = new Product();
7: product.ProductId = reader.GetInt32(\);
8: product.Name = reader.GetString(\);
9: products.Add(product);
10: }
多结果集
FluentData支持多结果集。也就是说,可以在一次数据库查询中返回多个查询结果。使用该特性的时候,记得使用类似下面的语句对查询语句进行包装。需要在查询结束后把连接关闭。
1: using (var command = Context.MultiResultSql)
2: {
3: List
4: @\
5: select * from Product;\
6:
7: List
8: }
执行第一个查询时,会从数据库取回数据,执行第二个查询的时候,FluentData可以判断出这是一个多结果集查询,所以会直接从第一个查询里获取需要的数据。 分页
1: List
2: .From(@\
3: inner join Category c on c.CategoryId = p.CategoryId\
4: .Where(\)
5: .OrderBy(\)
6: .Paging(1, 10).QueryMany();
调用 Paging(1, 10),会返回最先检索到的10个Product。
FluentData入门(五)—Insert, Update, Delete 插入数据 使用 SQL 语句:
1: int productId = Context.Sql(@\
2: values(@0, @1);\
3: .Parameters(\, 1)
4: .ExecuteReturnLastId
使用builder:
1: int productId = Context.Insert(\)
2: .Column(\, \)
3: .Column(\, 1)
4: .ExecuteReturnLastId
使用builder,并且自动映射
1: Product product = new Product();
2: product.Name = \;
3: product.CategoryId = 1;
4:
5: product.ProductId = Context.Insert
6: .AutoMap(x => x.ProductId)
7: .ExecuteReturnLastId
8:
将ProductId作为AutoMap方法的参数,是要指明ProductId不需要进行映射,因为它是一个数据库自增长字段。 更新数据 使用SQL语句:
1: int rowsAffected = Context.Sql(@\
2: where ProductId = @1\
3: .Parameters(\, 1)
4: .Execute();
使用builder:
1: int rowsAffected = Context.Update(\)
2: .Column(\, \)
3: .Where(\, 1)
4: .Execute();
使用builder,并且自动映射:
1: Product product = Context.Sql(@\
2: where ProductId = 1\
3: .QuerySingle
4: product.Name = \;
5:
6: int rowsAffected = Context.Update
7: .AutoMap(x => x.ProductId)
8: .Where(x => x.ProductId)
9: .Execute();
将ProductId作为AutoMap方法的参数,是要指明ProductId不需要进行映射,因为它不需要被更新。
Insert and update - common Fill method
1: var product = new Product();
2: product.Name = \;
3: product.CategoryId = 1;
4:
5: var insertBuilder = Context.Insert
6:
7: var updateBuilder = Context.Update
8:
9: public void FillBuilder(IInsertUpdateBuilder
10: {
11: builder.Column(x => x.Name);
12: builder.Column(x => x.CategoryId);
13: }
14:
15: Delete
删除数据 使用SQL语句:
1: int rowsAffected = Context.Sql(@\
2: where ProductId = 1\
3: .Execute();
使用builder:
1: int rowsAffected = Context.Delete(\)
2: .Where(\, 1)
3: .Execute();
FluentData入门(六)--存储过程和事务
存储过程 使用SQL语句: 1: var rowsAffected = Context.Sql(\)
2: .CommandType(DbCommandTypes.StoredProcedure)
3: .Parameter(\, 1)
4: .Parameter(\, \)
5: .Execute();
使用builder:
1: var rowsAffected = Context.StoredProcedure(\)
2: .Parameter(\, \)
3: .Parameter(\, 1).Execute();
使用builder,并且自动映射
1: var product = Context.Sql(\)
2: .QuerySingle
3:
4: product.Name = \;
5:
6: var rowsAffected = Context.StoredProcedure
7: .AutoMap(x => x.CategoryId).Execute();
使用Lambda表达式
1: var product = Context.Sql(\)
2: .QuerySingle
3: product.Name = \;
4:
5: var rowsAffected = Context.StoredProcedure
6: .Parameter(x => x.ProductId)
7: .Parameter(x => x.Name).Execute();
事务
FluentData 支持事务。如果使用事务,最好使用using语句将代码包起来,已保证连接会被关闭。默认的,如果查询过程发生异常,如事务不会被提交,会进行回滚。
1: using (var context = Context.UseTransaction(true))
2: {
3: context.Sql(\)
4: .Parameters(\, 1)
5: .Execute();
6:
7: context.Sql(\)
8: .Parameters(\, 2)
9: .Execute();
10:
11: context.Commit();
12: }
13:
实体工厂
实体工厂负责在自动映射的时候,生成POCO实例。如果需要生成复杂的实例,可以自定义实体工厂:
1: List
2: .Sql(\)
3: .QueryMany
4:
5: public class CustomEntityFactory : IEntityFactory
6: {
7: public virtual object Resolve(Type type)
8: {
9: return Activator.CreateInstance(type);
10: }
11: }
正在阅读:
FluentData入门06-18
最新部编版小学六年级语文小升初毕业考试试题(共6套,含参考答案)04-27
2018-2019学年江西省高安市第二中学高一上学期期中考试地理试题04-27
黄鹤楼12年原浆,包装分析06-18
批评意见07-10
中山大学生化真题1999-200501-27
开展“社会保障卡惠民服务季”活动工作方案08-03
市人民政府办公室关于印发《十堰市创建国家卫生城市健康教育工作实施方案》的通知03-18
2022干部作风大提升专项行动心得体会及专题研讨发言提纲03-27
教师进修学校工作总结参考04-09
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- FluentData
- 入门
- 2014年大连中考物理试卷及答案
- 国家科技攻关重大项目
- 2019年光稳定剂市场发展前景分析及供需格局研究预测报告
- 年产10000套生态装配式军营项目可行性研究报告
- 中国LED行业企业竞争分析(报告精选) - 图文
- 药物皮试液配制规范
- 2018ICP年报专用信息安全管理制度
- 青建管质字〔2011〕36号青岛市建管局
- 04258-1 Hi-RTK 手薄软件简易说明书
- 省政府关于公布省级行政许可实施主体及实施的行政许可项目的通知
- 安徽大学毕业论文范文
- 《最大的麦穗》第二课时教学设计
- 英语中表示颜色词语的特殊含义
- 16最大的麦穗教案解读
- 塑钢缠绕管施工方案 - 图文
- 对接电杆的排杆及焊接工艺
- 信息技术与学科整合活动小结
- 社区红十字会工作计划
- 小学六年级数学上册单元练习题集(全册)
- 植物学下册笔记