菜的像徐坤
排名
7
文章
192
粉丝
15
评论
16
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

FreeSql CodeFirst设计模式

5485人阅读 2022/1/4 16:11 总访问:1019197 评论:0 收藏:0 手机
分类: Free sql

前言

freesql Code First 设计模式

freesql 支持更多的数据库特性
而不只是支持基础的数据类型,这既是优点也是缺点,优点是充分利用数据库特性辅助开发,缺点是切换数据库变得困难。

如果实体类属性,与数据库表字段不完整映射的时候,未映射的字段有可能发生丢失。

FreeSql提供两种CodeFirst移迁方法,自动和手动。

自动同步实体结构到数据库,程序运行中检查实体表是否存在,然后迁移执行创建或修改

  1.                 IFreeSql fsql = new FreeSqlBuilder()
  2.                 //指定数据库类型以及数据库连接
  3.                 .UseConnectionString(DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=oa;Pooling=true;Min Pool Size=1;uid=sa;pwd=Aa123456;Max Pool Size=5;Encrypt=True;TrustServerCertificate=True;database=oa")
  4.                 //aop监听sql
  5.                 .UseMonitorCommand(cmd =>//执行前
  6.                 {
  7.                 }, (cmd, valueString) =>//执行后
  8.                 {
  9.                 })
  10.                 .UseAutoSyncStructure(true)//CodeFirst自动同步将实体同步到数据库结构(开发阶段必备),默认是true,正式环境请改为false
  11.                 .Build();

禁用迁移

当【实体类】对应的是数据库【视图】或者其他时,可通过 [Table(DisableSyncStructure = true)] 禁用指定的实体迁移操作。




freesql 不能为SQL server ,mysql创建数据库,需要自行创建数据库  

Code First创建表结构

一、单张表创建

创建数据表实体类
属性添加表名,主键,自增


  1.            //同步实体类型到数据库(单个)
  2.             _FreeSql.CodeFirst.SyncStructure<Student>();

二、创建多个表结构

定义一个表模型类,将每张表设置一个name

  1.  public class Model
  2.     {
  3.         [Table(Name = "oa.dbo.Teacher")]
  4.         public class Teacher 
  5.         {
  6.             [Column(IsPrimary = true, IsIdentity = true)]
  7.             //
  8.             public string TId { getset; }
  9.             //
  10.             public string Tname { getset; }
  11.             //
  12.             public string Tsex { getset; }
  13.             //
  14.             public DateTime Tage { getset; }
  15.         }
  16.         [Table(Name = "oa.dbo.Class")]
  17.         public class Class
  18.         {
  19.             [Column(IsPrimary = true, IsIdentity = true)]
  20.             //唯一ID
  21.             public string CId { getset; }
  22.             //名称
  23.             public string Cname { getset; }
  24.             //年级
  25.             public string CClass { getset; }
  26.         }
  27.    }

创建完成后通过反射拿到model类下的所有表模型

  1.         /// <summary>
  2.         /// 获取Model类下的所有数据库模型类,并拿到type集合
  3.         /// </summary>
  4.         /// <returns></returns>
  5.         public static Type[] GetTypesByTableAttribute()
  6.         {
  7.             List<Type> tableAssembies = new List<Type>();
  8.             foreach (Type type in Assembly.GetAssembly(typeof(Model)).GetExportedTypes())
  9.                 foreach (Attribute attribute in type.GetCustomAttributes())
  10.                     if (attribute is TableAttribute tableAttribute)
  11.                         if (tableAttribute.DisableSyncStructure == false)
  12.                             tableAssembies.Add(type);
  13.             return tableAssembies.ToArray();
  14.         }

最后执行

  1.            //同步实体类型到数据库(多个)
  2.             _FreeSql.CodeFirst.SyncStructure(GetTypesByTableAttribute());

三、命名空间创建

Entity文件夹下的实体类命名空间都是以WebApplication2.EntityList 命名



通过反射拿到同一命名空间下所有的实体类

  1. public static Type[] GetTypesByNameSpace(){
  2.     List<Type> tableAssembies = new List<Type>();
  3.     List<string> entitiesFullName = new List<string>()
  4.     {
  5.         "WebApplication2.EntityList",
  6.     };
  7.     foreach (Type type in Assembly.GetAssembly(typeof(Test1)).GetExportedTypes())
  8.         foreach (var fullname in entitiesFullName)
  9.             if (type.FullName.StartsWith(fullname) && type.IsClass)
  10.                 tableAssembies.Add(type);
  11.     return tableAssembies.ToArray();}

最后执行

  1.            //同步实体类型到数据库(多个)
  2.             _FreeSql.CodeFirst.SyncStructure(GetTypesByNameSpace());


更多请查看freesql官方文档
指南 | FreeSql 官方文档

评价