Skip to content
IGeekFan 开发者文档IGeekFan 开发者文档
CMS
.NET指南
FreeKit
Docker
关于
博客
github icon
    • .NET Core 学习示例文档
      • 创建简单Hello World
        • .NET Core 简单测试项目
          • FreeSql+ASP.NET Core
            • 文章概述
              • FreeSql 简介
                • 源码
                  • 参考
                    • 项目准备
                      • 创建项目
                      • Install
                        • code first
                          • db first
                            • 开始
                              • 分析需求
                                • 基础介绍
                                  • 自动同步实体结构【开发环境必备】
                                • RESTful+FreeSql+AutoMapper
                                  • IdentityServer4
                                    • 七牛云对象存储
                                      • ImCore 即时通讯
                                        • Nacos 配置中心
                                          • Serilog指南
                                            • 在 ASP.NET Core 6 如何添加 Startup.cs
                                              • 在 ASP.NET Core 6 中添加Startup.cs的整洁方式

                                              FreeSql+ASP.NET Core

                                              calendar icon2020年5月8日timer icon大约 6 分钟word icon约 1715 字

                                              此页内容
                                              • 文章概述
                                              • FreeSql 简介
                                              • 源码
                                              • 参考
                                              • 项目准备
                                                • 创建项目
                                              • Install
                                              • code first
                                              • db first
                                              • 开始
                                                • 分析需求
                                                • 基础介绍
                                                • 自动同步实体结构【开发环境必备】

                                              # FreeSql+ASP.NET Core

                                              # 文章概述

                                              主要在介绍FreeSql在ASP.NTE Core WebApi中如何使用的过程,完成一个最简单的博客系统的后端接口。

                                              # FreeSql 简介

                                              国人写的一个功能强大的ORM,FreeSql 支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite,特点:轻量级、可扩展、基于 .NET Standard 跨平台。

                                              # 源码

                                              代码托管在GitHub上 https://github.com/luoyunchong/dotnetcore-examplesopen in new window

                                              # 参考

                                              • FreeSql github https://github.com/2881099/FreeSqlopen in new window
                                              • 关于.net core cli中如何使用dotnet newopen in new window
                                              • 使用 ASP.NET Core 创建 Web APIopen in new window
                                              • Swagger/OpenAPI 生成接口文档open in new window
                                              • Swagger GitHub (Swashbuckle.AspNetCore)open in new window

                                              # 项目准备

                                              • Mysql 5.6
                                              • Visual Studio 2019或2017、Visual Studio code
                                              • .NET Core 3.1+
                                              • PowerShell
                                              • 懂点mvc,该教程不会教你如何使用 ASP .NET Core MVC、RESTful

                                              # 创建项目

                                              使用dotnet 命令行创建一个webapi项目,起名为OvOv.FreeSql

                                              PS dotnetcore-examples\aspnetcore-freesql> dotnet new webapi -n OvOv.FreeSql
                                              The template "ASP.NET Core Web API" was created successfully.
                                              
                                              1
                                              2

                                              然后cd 到OvOv.FreeSql目录,通过dotnet run 命令运行项目

                                              PS dotnetcore-examples\aspnetcore-freesql> cd .\OvOv.FreeSql\
                                              PS dotnetcore-examples\aspnetcore-freesql\OvOv.FreeSql> dotnet run
                                              
                                              info: Microsoft.Hosting.Lifetime[0]
                                                    Now listening on: https://localhost:5001
                                              info: Microsoft.Hosting.Lifetime[0]
                                                    Now listening on: http://localhost:5000
                                              info: Microsoft.Hosting.Lifetime[0]
                                                    Application started. Press Ctrl+C to shut down.
                                              info: Microsoft.Hosting.Lifetime[0]
                                                    Hosting environment: Development
                                              info: Microsoft.Hosting.Lifetime[0]
                                                    Content root path: D:\code\github\dotnetcore-examples\aspnetcore-freesql\OvOv.FreeSql
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6
                                              7
                                              8
                                              9
                                              10
                                              11
                                              12
                                              13

                                              打开浏览器 https://localhost:5001 会出现404

                                              请打开这个地址 https://localhost:5001/api/values ,可看到如下内容。

                                              ["value1","value2"]
                                              
                                              1

                                              接下来我们来集成FreeSql,我们以最简单的命令和说明,详细内容去官网看具体内容

                                              • 官网文档 http://freesql.net/docopen in new window

                                              # Install

                                              要先cd到OvOv.FreeSql目录中。

                                              PS \aspnetcore-freesql\OvOv.FreeSql> dotnet add package FreeSql
                                              PS \aspnetcore-freesql\OvOv.FreeSql> dotnet add package FreeSql.Provider.MySql
                                              
                                              1
                                              2

                                              # code first

                                              • 关于CodeFirst,官方文档的介绍open in new window

                                              代码优先,使用过EntityFramework的应该很清楚这一概念,我的理解就是:在分析数据库表关系时,不通过在数据库中设计表,而是直接在代码中声明对应的类,使用导航属性代替外键关联,通过数据表字段与C#中的类库对应,从而自动生成数据表。

                                              # db first

                                              数据库优先:需求分析后,直接设计数据库,通过数据库中的表,直接生成代码,类。

                                              # 开始

                                              # 分析需求

                                              我们以code first 为示例,学习如何使用freesql,实现一个简单的博客。将表内容分为博客表(Blog)和评论表(Post)

                                              # Blog 表

                                              字段名字段类型说明
                                              BlogIdint博客id
                                              Titlevarchar(50)博客标题
                                              Contentvarchar(500)博客内容
                                              CreateTimeDateTime发布时间

                                              # Post 表

                                              字段名字段类型说明
                                              PostIdint评论id
                                              ReplyContentvarchar(50)标题
                                              BlogIdint博客id
                                              ReplyTimeDateTime回复时间

                                              建一个Domain文件夹,用于存放数据库表中对应的实体类。

                                              # 基础介绍

                                              # 1. Column属性介绍,大家可以看源码,解析open in new window

                                              1). 比如:Blog表中指定了Title为varchar(50),我们如何通过代码指定了主键,唯一值,字形。

                                                  public class Blog
                                                  {
                                                      [Column(IsIdentity = true, IsPrimary = true)]
                                                      public int BlogId { get; set; }
                                                      [Column(DbType = "varchar(50)")]
                                                      public string Title { get; set; }
                                                  }
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6
                                              7

                                              2). Column的命名空间在

                                              using FreeSql.DataAnnotations;
                                              
                                              1

                                              更多属性介绍

                                              字段备注
                                              Name数据库列名
                                              OldName指定数据库旧的列名,修改实体属性命名时,同时设置此参数为修改之前的值,CodeFirst才可以正确修改数据库字段;否则将视为【新增字段】
                                              DbType数据库类型,如: varchar(255)
                                              IsPrimary主键
                                              IsIdentity自增标识
                                              IsNullable是否可DBNull
                                              IsIgnore忽略此列,不迁移、不插入
                                              IsVersion设置行锁(乐观锁)版本号,每次更新累加版本号,若更新整个实体时会附带当前的版本号判断(修改失败时抛出异常)
                                              DbDefautValue数据库默认值
                                              MapType类型映射,比如:可将 enum 属性映射成 typeof(string)
                                              Uniques唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。

                                              # 2. Table 的使用:用于在类的上面指定这个表的属性

                                              [Table(Name = "t_blog")]
                                              public class Blog {
                                                //...
                                              }
                                              
                                              1
                                              2
                                              3
                                              4

                                              更多属性介绍

                                              字段备注
                                              Name数据库表名
                                              OldName指定数据库旧的表名,修改实体命名时,同时设置此参数为修改之前的值,CodeFirst才可以正确修改数据库表;否则将视为【创建新表】
                                              SelectFilter查询过滤SQL,实现类似 a.IsDeleted = 1 功能
                                              DisableSyncStructure禁用 CodeFirst 同步结构迁移

                                              # 3. 其他的还是看 https://github.com/2881099/FreeSql/blob/master/Docs/codefirst.md

                                              # Blog.cs

                                              using FreeSql.DataAnnotations;
                                              using System;
                                              
                                              namespace OvOv.FreeSql.Domain
                                              {
                                                  public class Blog
                                                  {
                                                      [Column(IsIdentity = true, IsPrimary = true)]
                                                      public int BlogId { get; set; }
                                                      [Column(DbType = "varchar(50)")]
                                                      public string Title { get; set; }
                                                      [Column(DbType = "varchar(500)")]
                                                      public string Content { get; set; }
                                                      public DateTime CreateTime { get; set; }
                                              
                                              
                                                  }
                                              }
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6
                                              7
                                              8
                                              9
                                              10
                                              11
                                              12
                                              13
                                              14
                                              15
                                              16
                                              17
                                              18

                                              # Post.cs

                                              
                                              using FreeSql.DataAnnotations;
                                              using System;
                                              
                                              namespace OvOv.FreeSql.Domain
                                              {
                                                  public class Post
                                                  {
                                                      [Column(IsIdentity = true, IsPrimary = true)]
                                                      public int PostId { get; set; }
                                                      [Column(DbType = "varchar(50)")]
                                                      public string ReplyContent { get; set; }
                                                      public int BlogId { get; set; }
                                                      public DateTime ReplyTime { get; set; }
                                                      public Blog Blog { get; set; }
                                                  }
                                              }
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6
                                              7
                                              8
                                              9
                                              10
                                              11
                                              12
                                              13
                                              14
                                              15
                                              16
                                              17

                                              # Startup.cs

                                              非全部代码,这里注意点:要先在mysql中创建数据库FreeSql_Blog,否则一直提示主库xxxxx,官网未找到相关描述。

                                              这里初始化FreeSql,并使用单例模式,注入到默认的依赖中,这样在Controller中即可直接注入。

                                              namespace OvOv.FreeSql
                                              {
                                                  public class Startup
                                                  {
                                                      public Startup(IConfiguration configuration)
                                                      {
                                                          Fsql = new FreeSqlBuilder()
                                                                      .UseConnectionString(DataType.MySql, @"Data Source=127.0.0.1;Port=3306;User ID=root;Password=123456;Initial Catalog=FreeSql_Blog;Charset=utf8;SslMode=none;Max pool size=10")
                                                                      .UseAutoSyncStructure(true)
                                                                      .Build();
                                                      }
                                              
                                                      public IFreeSql Fsql { get; }
                                              
                                                      public void ConfigureServices(IServiceCollection services)
                                                      {
                                                          services.AddSingleton<IFreeSql>(Fsql);
                                              
                                                      }
                                                  }
                                              }
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6
                                              7
                                              8
                                              9
                                              10
                                              11
                                              12
                                              13
                                              14
                                              15
                                              16
                                              17
                                              18
                                              19
                                              20
                                              21

                                              # BlogController

                                              在controllers文件夹新建一个控制器BlogController

                                              using System;
                                              using System.Collections.Generic;
                                              using System.Linq;
                                              using System.Threading.Tasks;
                                              using FreeSql;
                                              using Microsoft.AspNetCore.Mvc;
                                              using OvOv.FreeSql.Domain;
                                              
                                              namespace OvOv.FreeSql.Controllers
                                              {
                                                  [Route("api/[controller]")]
                                                  [ApiController]
                                                  public class BlogController : ControllerBase
                                                  {
                                                      // GET api/Blog
                                              
                                                      IFreeSql _fsql;
                                                      public BlogController(IFreeSql fsql)
                                                      {
                                                          _fsql = fsql;
                                                      }
                                              
                                                      [HttpGet]
                                                      public ActionResult<IEnumerable<Blog>> Get()
                                                      {
                                                          List<Blog> blogs = _fsql.Select<Blog>().OrderByDescending(r => r.CreateTime).ToList();
                                              
                                                          return blogs;
                                                      }
                                              
                                                      // GET api/blog/5
                                                      [HttpGet("{id}")]
                                                      public ActionResult<Blog> Get(int id)
                                                      {
                                                          return _fsql.Select<Blog>(id).ToOne();
                                                      }
                                              
                                              
                                                      // DELETE api/blog/5
                                                      [HttpDelete("{id}")]
                                                      public void Delete(int id)
                                                      {
                                                          _fsql.Delete<Blog>(new { BlogId = id }).ExecuteAffrows();
                                                      }
                                                  }
                                              }
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6
                                              7
                                              8
                                              9
                                              10
                                              11
                                              12
                                              13
                                              14
                                              15
                                              16
                                              17
                                              18
                                              19
                                              20
                                              21
                                              22
                                              23
                                              24
                                              25
                                              26
                                              27
                                              28
                                              29
                                              30
                                              31
                                              32
                                              33
                                              34
                                              35
                                              36
                                              37
                                              38
                                              39
                                              40
                                              41
                                              42
                                              43
                                              44
                                              45
                                              46

                                              重新运行,打开地址 http://localhost:5001/api/blog 会发现数据库中生成了表blog,这时候表post并没有生成。所以我们判断,只有在访问到实体类才检查是否存在表结构,然后执行相应的处理。

                                              手动向blog表中加一些数据,然后再次请求

                                              • http://localhost:5001/api/blog, 可看到相应的数据。
                                              • http://localhost:5001/api/blog/1 可得到单个数据。

                                              # 自动同步实体结构【开发环境必备】

                                              此功能默认为开启状态,发布正式环境后,请修改此设置

                                              Fsql = new FreeSqlBuilder()
                                                        .UseConnectionString(DataType.MySql, @"连接字符串")
                                                        .UseAutoSyncStructure(true)
                                                        .Build();
                                                                    
                                              //UseAutoSyncStructure(true/false)【开发环境必备】自动同步实体结构到数据库,程序运行中检查实体表是否存在,然后创建或修改
                                              
                                              // 也可使用此方法指定是否自动同步结构。                  
                                              Fsql.CodeFirst.IsAutoSyncStructure = true;
                                              
                                              1
                                              2
                                              3
                                              4
                                              5
                                              6
                                              7
                                              8
                                              9
                                              edit icon在 GitHub 上编辑此页open in new window
                                              上次编辑于: 2022/6/29 19:17:23
                                              贡献者: igeekfan,luoyunchong
                                              上一页
                                              .NET Core 简单测试项目
                                              下一页
                                              RESTful+FreeSql+AutoMapper
                                              MIT Licensed | Copyright © 2021-present luoyunchong
                                              苏ICP备16046457号-1

                                              该应用可以安装在你的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和你的操作系统安全地进行交互。

                                              详情