1 Star 1 Fork 1

2881099 / dotnetGen_sqlserver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

.NETCore 2.1 + SQLServer 生成器

作者面向web应用开发13年,在项目实践与学习中去旧换新,吸取优点,从高质量、强规范、快速开发方向累积,形成生成器工具减少项目后端开发难度。

navicat 模型工具创建和管理ER图,从数据库导入/同步结构到数据库,后使用本生成器一键同步c#实体,以及各种规范的花式和你想象不到的语法,支持缓存,支持数据库99%类型,挖掘数据库特性,避免过多重复劳动和不规范、不健壮的代码。

优势:

  • 1、根据主键、唯一键、外键(1对1,1对多,多对多)生成功能丰富的数据库 SDK;
  • 2、严格把控数据库,避免随意创建表或字段,有标准的ER图数据库规范;
  • 3、统一规范数据库操作类与方法(相比EF太随意的用法好管理很多),一条心堆业务;
  • 4、优化每一个细节,决不允许低级错误干扰我们的业务逻辑(相比ORM,生成的代码更专注);

适合场境:

  • 1、新项目开发,只管设计数据ER图,无须考虑db相关代码;
  • 2、老项目数据库访问方式不堪入目的,表数量越多收益越大;

下载生成器winform客户端,推荐安装命令工具 dotnet tool install -g GenMs

学习QQ群:8578575


在已有的项目上生成

dotnet new mvc
GenMs 服务器 -U 登陆名 -P 密码 -D 数据库1 -N 命名空间
#GenMs 服务器 -D 数据库1 -N 命名空间
#如不提供 -U 参数,则使用管理员登陆

生成完整的模块化解决方案

GenMs 服务器 -U 登陆名 -P 密码 -D 数据库1 -N 命名空间 -R -S -A

dotnetGen 保持相同的开发与使用习惯,实现了面向 mysql、SQLServer、PostgreSQL 三种数据库快速开发,也可混合使用。

功能对比 dotnetGen_mysql dotnetGen_sqlserver dotnetGen_postgresql
windows
linux
连接池
事务
多数据库 - -
读写分离
表关系(1对1)
表关系(1对多)
表关系(多对多)
表主键
表唯一键
存储过程 - -
视图
软删除
类型映射
枚举 -
自定义类型 - -
gis -
数组 - -
字典 - -
xml - - -
json - -
缓存
命令行生成
RESTful
后台管理功能

模块化框架目录结构介绍

Module

所有业务接口约定在 Module 划分并行开发,互不依赖

Module/Admin
生成的后台管理模块,http://localhost:5001/module/Admin 可访问

Module/Test
生成的测试模块

WebHost

WebHost 编译的时候,会将 Module/* 编译结果复制到当前目录
WebHost 只当做主引擎运行时按需加载相应的 Module
WebHost 依赖 npm ,请安装 node,并在目录执行 npm install
WebHost 依赖 gulp-cli,请执行全局安装 npm install --global gulp-cli
运行步骤:
1、打开 vs 右击 Module 目录全部编译;
2、cd WebHost && npm install && dotnet build && dotnet run

Infrastructure

Module 里面每个子模块的依赖所需

xx.db

包含一切数据库操作的封装
xx.Model(实体映射)
xx.BLL(静态方法封装)
xx.DAL(数据访问)
生成名特征取数据库名首字母大写(如: 表 test 对应 xx.Model.TestInfo、xx.BLL.Test、xx.DAL.Test)

数据库设计命名习惯:所有命名(username, stats_click)、外键字段(user_id)
仅支持主键作为外键,不支持组合字段,不支持唯一键作为外键
修改数据库后,双击“./GenMs只更新db.bat”可快速覆盖,所有类都使用 partial,方便扩展亦不会被二次生成覆盖

数据库相关方法

添加记录

// 如有 create_time 字段并且类型为日期,内部会初始化
TestInfo newitem1 = Test.Insert(Title: "添加的标题", Content: "这是一段添加的内容");
TestInfo newitem2 = Test.Insert(new TestInfo { Title = "添加的标题", Content = "这是一段添加的内容" });

添加记录(批量)

List<TestInfo> newitems1 = Test.Insert(new [] {
	new TestInfo { Title = "添加的标题1", Content = "这是一段添加的内容1" },
	new TestInfo { Title = "添加的标题2", Content = "这是一段添加的内容2" }
});

更新记录

// 更新 id = 1 所有字段
Test.Update(new TestInfo { Id: 1, Title = "添加的标题", Content = "这是一段添加的内容", Clicks = 1 });
// 更新 id = 1 指定字段
Test.UpdateDiy(1).SetTitle("修改后的标题").SetContent("修改后的内容").SetClicks(1).ExecuteNonQuery();
// update 表名 set clicks = clicks + 1 where id = 1
Test.UpdateDiy(1).SetClicksIncrement(1).ExecuteNonQuery();
// 使用实体层修改
new TestInfo { Id = 1 }.UpdateDiy.SetClicksIncrement(1).ExecuteNonQuery();

更新记录(批量)

//先查找 clicks 在 0 - 100 的记录
List<TestInfo> newitems1 = Test.Select.WhereClicksRange(0, 100).ToList();
// update 表名 set clicks = clicks + 1 where id in (newitems1所有id)
newitems1.UpdateDiy().SetClicksIncrement(1).ExecuteNonQuery();

警告:批量更新的方法,在事务中使用会导致死锁

删除记录

// 删除 id = 1 的记录
Test.Delete(1);

按主键/唯一键获取单条记录

appsettings可配置缓存时间,以上所有增、改、删都会删除缓存保障同步

//按主键获取
UserInfo user1 = User.GetItem(1);
//按唯一键
UserInfo user2 = User.GetItemByUsername("2881099@qq.com");
// 返回 null 或 UserInfo

查询(核心)

//BLL.表名.Select 是一个链式查询对象,几乎支持所有查询,包括 group by、inner join等等,最终 ToList ToOne Aggregate 执行 sql
List<UserInfo> users1 = User.Select.WhereUsername("2881099@qq.com").WherePassword("******").WhereStatus(正常).ToList();
//返回 new List<UserInfo>() 或 有元素的 List,永不返回 null

//返回指定列,返回List<元组>
var users2 = User.Select.WhereStatus(正常).Aggregate<(int id, string title)>("id,title");

//多表查询,只返回 a 表字段
var users3 = User.Select.Where(a => a.Obj_user_group.Id == a.Group_id).ToList();

//join查询,返回 a, b 表字段 ,b 表结果填充至 a.Obj_user_group 对象,类似 ef.Include
var users4 = User.Select.InnerJoin(a => a.Obj_user_group.Id == a.Group_id).ToList();

//分组查询
var users5 = User.Select.GroupBy("group_id").Aggregate<(int groupId, int count)>("group_id, count(1)");

//等等...

事务

//错误会回滚,事务内支持所有生成的同步方法(不支持生成对应的Async方法)
var user = User.GetItem(1);
SqlHelper.Transaction(() => {
	if (user.UpdateDiy.SetAmountIncrement(-num).Where("amount > {0}", num).ExecuteNonQuery() <= 0)
		throw new Exception("余额不足");

	var order = user.AddOrder(Amount: 1, Count: num, Count_off: num);
});

缓存

1、根据主键、唯一键缓存

BLL GetItem、GetItemBy唯一键,使用了默认缓存策略180秒,用来缓存一条记录,db 层自动维护缓存同步,例如:

//只有第一次查询了数据库,后面99次读取redis的缓存值
UserInfo u;
for (var a = 0; a < 100; a++)
	u = User.GetItemByUsername("2881099@qq.com");

//执行类似以下的数据变动方法,会删除redis对应的缓存
u.UpdateDiy.SetLogin_time(DateTime.Now).ExecuteNonQuery();

2、缓存一个查询结果

BLL Select.ToList(10, "cache_key"),将查询结果缓存10秒,需要手工删除redis对应的键

读写分离

内置实现读和写分离,一个【主库】多个【从库】,【从库】的查询策略为随机方式。

若某【从库】发生故障,将切换到其他可用【从库】,若已全部不可用则使用【主库】查询。

出现故障【从库】被隔离起来间隔性的检查可用状态,以待恢复。

Topic.Select.WhereId(1).ToOne(); //读【从库】(默认)
Topic.Select.Master().WhereId(1).ToOne(); //读【主库】

测试

生成规则

不会生成

  • 没有主键,不会生成 增、改、删 方法
  • 有自增字段,不会生成 批量 Insert 方法

特别规则

  • 字段类型 string 相关并且长度 <= 300,会生成

    表.Select.Where字段Like

  • 95%的数据类型被支持
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2018 Ye Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

.NETCore + SqlServer 生成器 展开 收起
C#
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/FreeSql/dotnetGen_sqlserver.git
git@gitee.com:FreeSql/dotnetGen_sqlserver.git
FreeSql
dotnetGen_sqlserver
dotnetGen_sqlserver
master

搜索帮助