简介:SqlRepoEx是 .Net平台下兼容.NET Standard 2.0人一个轻型的ORM。解决了Lambda转Sql语句这一难题,SqlRepoEx使用的是Lambda表达式,所以,对c#程序员来说,是非常简单的,其语法特点与Linq to Sql极为相似。不仅实现了完整的Select、Insert、Update、Delete等语句解析器,同时,也实现了Select、where、order by等子句,这些语句与子句均支持导出SQL语句,使得拼接复杂场景SQL语句变得轻松,SqlRepoEx很快其原生数据访问与Dapper不相上下,SqlRepoEx本身支持Sql Server与MySql方言,同时通过SqlRepoEx.Normal支持非方言SQL。SqlRepoEx没侵入性,仅通过简单的几个特性,就能让类与数据库关联起来;

*本系列以静态工厂为例;

*数据来源于Northwind数据库;

*完整的代码见 https://github.com/AzThinker/SqlRepoEx2.0StartGuid  https://gitee.com/azthinker/SqlRepoEx2.0StartGuid

1、从 SqlRepoEx 2.2.1 起,标识数据据字段的特性标识改为使用System.ComponentModel.DataAnnotations中的声明,降低使用难度,当然 SqlRepoEx 中自定义的特性依然是支持的。

2、只有以类属性定义的,才能作为数据字段使用, SqlRepoEx 只反身属性。

3、SqlRepoEx 不使用XML文件来映射关系

一、表名

1、如果不使用任何特性, SqlRepoEx会将当前类名当成数名。

如:

public sealed class AzProducts

{

...

}

会返回形如  select ...  from AzProducts

显然,AzCategories在数据库中不存在,当然,如果类名与数据库中对应的表名一至,是可以不用标识的。

2、使用 Table 特性,定义在System.ComponentModel.DataAnnotations中

[Table("Products")]

public sealed class AzProducts

{

...

}

会返回形如  select ...  from Products

3、使用 TableName特性,定义在SqlRepoEx.Core.CustomAttribute中

[TableName("Products")]

public sealed class AzProducts

{

...

}

同样会返回形如  select ...  from Products

当然,使用哪种,按各自的喜好

二、关键字段

1、如果未使用任何特性,在使用,For() 方法时,不会自动的产生Where条件语句。

[Table("Products")]

public sealed class AzProducts

{

public int ProductID { get; set; }

...

}

当使用

AzProducts azProducts = new AzProducts { ProductName2 = "testvalue", ProductID = 82 };

var resultUpdate = repository.Delete().For(azProducts);

只会产生

DELETE [dbo].[Products]

当然,这不所期望的

2、使用 Key 特性 Table 定义在System.ComponentModel.DataAnnotations中

[Table("Products")]

public sealed class AzProducts

{

[Key]

public int ProductID { get; set; }

...

}

会返回形如 DELETE [dbo].[Products] WHERE  [ProductID] = 82;

3、使用 KeyField 特性 Table定义在SqlRepoEx.Core.CustomAttribute中

[TableName("Products")]

public sealed class AzProducts

{

[KeyField]

public int ProductID { get; set; }

...

}

同样会返回形如 DELETE [dbo].[Products] WHERE  [ProductID] = 82;

三、标识字段(自增字段)

1、如果未使用任何特性,在使用,For() 方法时,在增加操作时,不会返回自增自段的实际值。

[Table("Products")]

public sealed class AzProducts

{

public int ProductID { get; set; }

...

}

当使用

AzProducts azProducts = new AzProducts { ProductName2 = "testvalue" };

var resultinsert = repository

.Insert()

.For(azProducts);

只会产生

INSERT [dbo].[Products]([ProductName],[SupplierID],[CategoryID],[QuantityPerUnit],[UnitPrice],[UnitsInStock],[UnitsOnOrder],[ReorderLevel],[Discontinued])

VALUES('testvalue',NULL,NULL,NULL,NULL,NULL,NULL,NULL,0);

2、使用 DatabaseGenerated 特性,定义在System.ComponentModel.DataAnnotations中

[Table("Products")]

public sealed class AzProducts

{

[Key]

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

public int ProductID { get; set; }

...

}

会返回形如

INSERT [dbo].[Products]([ProductName],[SupplierID],[CategoryID],[QuantityPerUnit],[UnitPrice],[UnitsInStock],[UnitsOnOrder],[ReorderLevel],[Discontinued])

VALUES('testvalue',NULL,NULL,NULL,NULL,NULL,NULL,NULL,0);

SELECT [ProductID],[ProductName] as ProductName2,[SupplierID],[CategoryID],[QuantityPerUnit],[UnitPrice],[UnitsInStock],[UnitsOnOrder],[ReorderLevel],[Discontinued]

FROM [dbo].[Products]

WHERE [ProductID] = SCOPE_IDENTITY();

3、使用 IdentityField 特性,定义在SqlRepoEx.Core.CustomAttribute中

[TableName("Products")]

public sealed class AzProducts

{

[KeyField]

[IdentityField]

public int ProductID { get; set; }

...

}

同样会返回形如

INSERT [dbo].[Products]([ProductName],[SupplierID],[CategoryID],[QuantityPerUnit],[UnitPrice],[UnitsInStock],[UnitsOnOrder],[ReorderLevel],[Discontinued])

VALUES('testvalue',NULL,NULL,NULL,NULL,NULL,NULL,NULL,0);

SELECT [ProductID],[ProductName] as ProductName2,[SupplierID],[CategoryID],[QuantityPerUnit],[UnitPrice],[UnitsInStock],[UnitsOnOrder],[ReorderLevel],[Discontinued]

FROM [dbo].[Products]

WHERE [ProductID] = SCOPE_IDENTITY();

四、非字段属性

1、如果未使用任何特性,如下面属性 public string Supplier { get; set; }。此属性在数据库中无对应的字段

[Table("Products")]

public sealed class AzProducts

{

[Key]

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

public int ProductID { get; set; }

public string Supplier { get; set; }

...

}

当使用

var result = repository.Query();

会产生形如

select ProductID,Supplier ....

2、使用 NotMapped 特性,定义在System.ComponentModel.DataAnnotations中

[Table("Products")]

public sealed class AzProducts

{

[Key]

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

public int ProductID { get; set; }

[NotMapped]

public string Supplier { get; set; }

...

}

当使用

var result = repository.Query();

时,Supplier 不会出现在查询语句中,但要注意,当使用选择器来查询时,何然会出现在Select语句中;

如 var result = repository.Query().Select(c=>c.ProductID,c=>c.Supplier);

3、使用 NonDatabaseField 特性,定义在SqlRepoEx.Core.CustomAttribute中

[TableName("Products")]

public sealed class AzProducts

{

[Key]

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

public int ProductID { get; set; }

[NonDatabaseField]

public string Supplier { get; set; }

...

}

当使用

var result = repository.Query();

时,Supplier 不会出现在查询语句中,但要注意,当使用选择器来查询时,何然会出现在Select语句中;

如 var result = repository.Query().Select(c=>c.ProductID,c=>c.Supplier);

五、指定属性对应的数据库字段名

有时,类属性名与数据库中的字段名不完全是一一对应的,所以需要指定实际字段名

使用 Column 特性,定义在System.ComponentModel.DataAnnotations中,SqlRepoEx中没有对应的特性。

如下列类中的  ProductName2 属性,对应的数据库字段实为 ProductName

[TableName("Products")]

public sealed class AzProducts

{

[Key]

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

public int ProductID { get; set; }

[NonDatabaseField]

public string Supplier { get; set; }

[Column("ProductName")]

public string ProductName2 { get; set; }

...

}

轻量ORM-SqlRepoEx (十五)最佳实践之数据映射(Map)的更多相关文章

  1. 轻量ORM-SqlRepoEx (十三)最佳实践

    ORM-SqlRepoEx 是 .Net平台下兼容.NET Standard 2.0,一个实现以Lambda表达式转转换标准SQL语句,使用强类型操作数据的轻量级ORM工具,在减少魔法字串同时,通过灵 ...

  2. 采用轻量ServiceMesh实现灰度发布的实践

    软件总会有缺陷的,解决问题的同时往往会引入新的问题,关键是看这些问题是否在我们的控制范围内,“灰度发布”就是让问题受控的方法之一. 前言 我们的 CTO 经常说:“研发团队最首要的任务是提供稳定的服务 ...

  3. 软件设计师【软件工程:软件开发模型、XP极限编程十二最佳实践】

    一.软件开发模型 二.XP极限编程十二最佳实践

  4. 轻量ORM-SqlRepoEx (十六)最佳实践之Dapper(2)

    简介:SqlRepoEx是 .Net平台下兼容.NET Standard 2.0人一个轻型的ORM.解决了Lambda转Sql语句这一难题,SqlRepoEx使用的是Lambda表达式,所以,对c#程 ...

  5. Dapper.NET——轻量ORM

    Dapper.NET使用 http://www.cnblogs.com/yankliu-vip/p/4182892.html 本文目录 Dapper.NET使用 1.为什么选择Dapper 2.以Da ...

  6. SqlSugar轻量ORM

      蓝灯软件数据股份有限公司项目,代码开源. SqlSugar是一款轻量级的MSSQL ORM ,除了具有媲美ADO的性能外还具有和EF相似简单易用的语法. 学习列表 0.功能更新 1.SqlSuga ...

  7. Dapper.NET—轻量ORM

    Dapper.NET使用 本文目录 Dapper.NET使用 1.为什么选择Dapper 2.以Dapper(4.0)为例. 2.1 在数据库中建立几张表. 2.2实体类. 3.使用方法 3.1  一 ...

  8. [转] Jenkins Pipeline插件十大最佳实践

    [From] http://blog.didispace.com/jenkins-pipeline-top-10-action/ Jenkins Pipeline 插件对于 Jenkins 用户来说可 ...

  9. 轻量ORM-SqlRepoEx (十四)最佳实践之Dapper(1)

    简介:SqlRepoEx是 .Net平台下兼容.NET Standard 2.0人一个轻型的ORM.解决了Lambda转Sql语句这一难题,SqlRepoEx使用的是Lambda表达式,所以,对c#程 ...

随机推荐

  1. 前端动画小记---bilibili ( ゜-゜)つロ客户下载小动画

    逛哔哩哔哩 ( ゜-゜)つロPC版的时候看到一个蛮有意思的动画,指导用户去下载客户端,于是摸索实现了一个. 原动画效果 可以看到,一个静止的小电视人,当鼠标移动到电视人身上时,电视人慢慢变身成为一个小 ...

  2. Python基础(一) - 数据类型及运算符

    基本数据类型 整数(int) 浮点数(float) 字符串 以' '或" " 括起来的任意文本.  a. 如果'本身也是字符,可以用" "括起来    prin ...

  3. MD5计算器

    private void radioBtnFlie_CheckedChanged(object sender, EventArgs e) { RadioButton rb = sender as Ra ...

  4. (C# Window Service) Verify that you have sufficient privileges to start system services

    写了个Windows Service, 用Wix 写了个Installer,编译通过,生成了msi 安装文件,但是安装的时候总是提示: Product: KingPro Service -- Erro ...

  5. python oop常用术语 继承 多态 封装

    面向对象优点 1.通过封装明确了内外 2.通过继承+多态在语言层面支持了归一化设计 抽象/实现 抽象指对现实世界问题和实体的本质表现,行为和特征建模,建立一个相关的子集,可以用于 绘程序结构,从而实现 ...

  6. css预处理器(sass)

    学过CSS的人都知道,它不是一种编程语言.你可以用它开发网页样式,但是没法用它编程.也就是说,CSS基本上是设计师的工具,不是程序员的工具.在程序员眼里,CSS是一件很麻烦的东西.它没有变量,也没有条 ...

  7. 几种常见的Windows 服务器无法联网/无法连接远程桌面等故障解决方案

    SEO优化扫我一.服务器无法连接远程桌面 1.Ping不通IP,网站打不开,不可以远程连接.可能是服务器死机了,或者网络有问题,请尝试Web重启服务器或联系服务商确认. 2.Ping正常,网站可以打开 ...

  8. The Shapes of CSS(css的形状)

    All of the below use only a single HTML element. Any kind of CSS goes, as long as it's supported in ...

  9. 乘风破浪:LeetCode真题_021_Merge Two Sorted Lists

    乘风破浪:LeetCode真题_021_Merge Two Sorted Lists 一.前言 关于链表的合并操作我们是非常熟悉的了,下面我们再温故一下将两个有序链表合并成一个的过程,这是基本功. 二 ...

  10. December 27th 2016 Week 53rd Tuesday

    A journey of one thousand miles begins with one step. 千里之行始于足下. No matter how slowly you walk, as lo ...