linux 网站301,济南电视台新闻频道,动漫制作专业学校有哪些,wordpress的留言功能.NET开源 ORM 框架 SqlSugar 系列
【开篇】.NET开源 ORM 框架 SqlSugar 系列【入门必看】.NET开源 ORM 框架 SqlSugar 系列【实体配置】.NET开源 ORM 框架 SqlSugar 系列【Db First】.NET开源 ORM 框架 SqlSugar 系列【Code First】.NET开源 ORM 框架 SqlSugar 系列 #x1f… .NET开源 ORM 框架 SqlSugar 系列
【开篇】.NET开源 ORM 框架 SqlSugar 系列【入门必看】.NET开源 ORM 框架 SqlSugar 系列【实体配置】.NET开源 ORM 框架 SqlSugar 系列【Db First】.NET开源 ORM 框架 SqlSugar 系列【Code First】.NET开源 ORM 框架 SqlSugar 系列 ORM实体的定义和作用 ORM实体通过映射关系将数据库表中的数据与应用程序中的对象关联起来。这种映射关系使得开发者可以通过操作对象来执行数据库操作如增加、删除、查询和更新数据而不需要直接编写SQL语句。 这样做的好处包括 简化数据库操作通过操作对象来执行数据库操作避免了直接编写SQL语句的复杂性。 提高开发效率ORM框架提供了丰富的API和工具使得数据库操作更加便捷和高效。 增强代码的可维护性通过对象关系映射代码与数据库的耦合度降低便于维护和升级。 1、实体使用自带特性 ☘️ 1.1 使用用例
对于 CRUD来说只需要配置主键和自增列就行了 如果类的名称和数据库不一样可以设置数据库中的名称。如果主键类型不是int类型就不需要设置自增下面有介绍比如GUID、雪花ID等等。
#主键自增#
SugarTable(dbstudent)]//当和数据库名称不一样可以设置表别名 指定表明
public class Student
{[SugarColumn(IsPrimaryKey true, IsIdentity true)]//数据库是自增才配自增 public int Id { get; set; }public int? SchoolId { get; set; }[SugarColumn(ColumnName StudentName)]//数据库与实体不一样设置列名 public string Name { get; set; }
}
#双主键、复合主键、联合主键、多个主键#
表中字段如果是自增需要加上 IsIdentity否则不需要
public class Student
{[SugarColumn(IsPrimaryKey true)] //设置主键public Guid Pk1{ get; set; }[SugarColumn(IsPrimaryKey true)] //设置主键public Guid Pk2{ get; set; }public string Name { get; set; }
}
#无主键#
当表中无主键的时候更新和删除需要指定条件具体用法看文档
public class Student
{public Guid Id{ get; set; }public string Name { get; set; }
} 总结只需要针对数据库中的 自增和主键进行设置即可和数据库一致 1.2 特性明细
下面是CRUD用到的特性不包含建表的属性 (建表看文档【迁移】) IsIdentity 是自增列 如果是Oracle12需要启用看文档: Oracle 如果是Oracle11及以下请设置OracleSequenceName 设置后和自增一样使用 IsPrimaryKey true是主键 ColumnName 实体类数据库列名不一样设置数据库列名 IsIgnore IsIgnoretrue表示 ORM 所有操作不处理这列 一般用于数据库没有这一列 ORM 非数据库列加上该特性配置导航查询自动IsIgnoretrue IsOnlyIgnoreInsert 插入操作时不处理该列 【插入中忽略】 对数据库默认值有效 IsOnlyIgnoreUpdate 更新操作不处理该列 【更新中忽略】 InsertServerTime 插入操作true数据库时间 UpdateServerTime 更新操作true数据库时间 InsertSql 插入根据SQL 等于0插入0 等 于a 插入a 等于 newid() 插入newid() 5.1.4.151支持了格式化 数据库函数({0}) UpdateSql 更新根据SQL 等于0更新0 等 于a 更新a 等于 newid() 更新newid() 5.1.4.151支持了格式化 数据库函数({0}) QuerySql 5.1.4.128 用于单表查询在没有使用Select下根据Sql生成 例如等于 Cast( Num_float64 as varchar(500)) 生成的Sql : Cast( Num_float64 as varchar(500)) AS Num_float64 一般用于orm驱动不支持的类型并且自定义类型也失效的情况 只在单表查询不使用Select的情况下才生效 注意 联表或者SELECT使用 函数或者扩展函数实现 OracleSequenceName 设置Oracle序列设置后该列等同于自增列 ,看文档: Oracle MaxParameterNameLength 一般等于30用于处理Oracle11 :参数化名字和索引名字超过30
建表看文档【迁移】这边只介绍CRUD用到的属性
2、实体使用自定义特性 ✅
下面是实现自定义特性的例子
SqlSugarClient db new SqlSugarClient(new ConnectionConfig()
{ConnectionString Config.ConnectionString,DbType DbType.SqlServer,IsAutoCloseConnection true,ConfigureExternalServices new ConfigureExternalServices(){EntityService (property, column) {var attributes property.GetCustomAttributes(true);//get all attributes if (attributes.Any(it it is KeyAttribute))// by attribute set primarykey{column.IsPrimarykey true; //有哪些特性可以看 1.2 特性明细}//可以写多个这边可以断点调试// if (attributes.Any(it it is NotMappedAttribute))//{// column.IsIgnore true; //}},EntityNameService (type, entity) {var attributes type.GetCustomAttributes(true);if (attributes.Any(it it is TableAttribute)){var attr(attributes.First(itit is TableAttribute) as TableAttribute);entity.DbTableName attr.Name;}}}
});[Table(student)]
//[SugarTable(student)]
public class MyStudent
{[Key]//[SugarColumn(IsPrimaryKey true)]public string Id { get; set; }public string Name { get; set; }
}
该功能非常强大远不止这点用法可以统一处理一些特性逻辑
3、实体不使用特性
3.1 无封装写法
根据规则来设置哪个是主键哪个是自增这样就不需要在实体加特性了(SqlSugar只需主键和自增就可以完成所有操作)。 推荐我个人推荐这样写假如当我们切换ORM框架时如果在实体配置了很多SqlSugar特有的特性时清理起来还是比较费时间的如果统一配置更改岂不是更省事您觉着呢 var db new SqlSugarClient(new ConnectionConfig()
{DbType SqlSugar.DbType.MySql,ConnectionString Config.ConnectionString,IsAutoCloseConnection true,ConfigureExternalServicesnew ConfigureExternalServices() {EntityService (t, column) {if (column.PropertyName.ToLower() id) //是id的设为主键{column.IsPrimarykey true;if (column.PropertyInfo.PropertyType typeof(int)) //是id并且是int的是自增{column.IsIdentity true;}}},EntityNameService (type, entity) {//entity.DbTableName 修改表名}
}
});
//根据你自个的逻辑去设置相应的主键和自增也可以从数据库读出主键和自增来动态设置
//db.DbMaintenance.GetColumnInfosByTableName 可以拿到表的信息
3.2 语法糖5.1.45
如果大量 if else 比较难看所以针对指定表进行了一些封
类似 fluent api
SqlSugarClient db new SqlSugarClient(new ConnectionConfig()
{DbType DbType.SqlServer,ConnectionString Config.ConnectionString3,InitKeyType InitKeyType.Attribute,IsAutoCloseConnection true,ConfigureExternalServices new ConfigureExternalServices(){EntityService (s, p) {//如果是Order实体进行相关配置p.IfTableOrder().UpdateProperty(it it.id, it {it.IsIdentity true;it.IsPrimarykey true;}).UpdateProperty(it it.Name, it {it.Length 100;it.IsNullable true;}).OneToOne(it it.Item, nameof(Order.ItemId));//如果Custom实体进行相关配置p.IfTableCustom().UpdateProperty(it it.id, it {it.IsIdentity true;it.IsPrimarykey true;}).UpdateProperty(it it.Text, it {it.DataType StaticConfig.CodeFirst_BigString;//支持多库的MaxString用法})//好处就是配置导航方便和针对指定表设置会方便些//可以结合全局逻辑一起使用如果下面逻辑和上面有冲突下面的会覆盖上面的//统一设置 nullable等于isnullabletrueif(p.IsPrimaryKeyfalsenew NullabilityInfoContext().Create(c).WriteState is NullabilityState.Nullable){p.IsNullable true;}},EntityNameService (type, entity) {//entity.DbTableName 修改表名}}
});
//性能说明
//EntityService 相同实体只会执行一次性不需太操作
4、迁移建表
✔️现在基本成熟点的后端框架都有Code First 功能主打一个开箱即可用。想当年我们还得手动创建数据库手动执行初始化脚本现在越来越人性化了。
public class CodeFirstTable1
{[SugarColumn(IsIdentity true, IsPrimaryKey true)]public int Id { get; set; } public string Name { get; set; }//ColumnDataType 一般用于单个库数据库如果多库不建议用[SugarColumn(ColumnDataType Nvarchar(255))]public string Text { get; set; }[SugarColumn(IsNullable true)]//可以为NULLpublic DateTime CreateTime { get; set; }
}//建表
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(CodeFirstTable1));
详细文档【Code First】.NET开源 ORM 框架 SqlSugar 系列
5、生成实体
✔️作为楼上的兄弟Db First 却肩负着另一项任务那就是先有数据库后根据数据表生成实体。很多小伙伴在开发项目时喜欢以数据库设计起手。还一个就是在线库表操作也需要用到这个技术实时同步更新实体做到不碰数据库也能维护库表。
//.net6以下
db.DbFirst.IsCreateAttribute().CreateClassFile(c:\\Demo\\1, Models);//.net6以上 string加?
db.DbFirst.IsCreateAttribute().StringNullable().CreateClassFile(c:\\Demo\\1, Models);
详细文档【Db First】.NET开源 ORM 框架 SqlSugar 系列
8、.NET6 非空string编译警告
不要使用 required 通过下面写法实现
public class Order
{[SqlSugar.SugarColumn(IsPrimaryKey true, IsIdentity true)]public int Id { get; set; }public string Name { get; set; } null!;//这样写
}