当前位置: 首页 > news >正文

wordpress建设的是模板网站吗迅雷磁力链bt磁力天堂下载

wordpress建设的是模板网站吗,迅雷磁力链bt磁力天堂下载,单页网站模板 带在线订单,物流公司.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 系列

  1. 【开篇】.NET开源 ORM 框架 SqlSugar 系列
  2. 【入门必看】.NET开源 ORM 框架 SqlSugar 系列
  3. 【实体配置】.NET开源 ORM 框架 SqlSugar 系列
  4. 【Db First】.NET开源 ORM 框架 SqlSugar 系列
  5. 【Code First】.NET开源 ORM 框架 SqlSugar 系列

🔸ORM实体的定义和作用:

ORM实体通过映射关系将数据库表中的数据与应用程序中的对象关联起来。这种映射关系使得开发者可以通过操作对象来执行数据库操作,如增加、删除、查询和更新数据,而不需要直接编写SQL语句。

🔸 这样做的好处包括:

  1.  简化数据库操作‌:通过操作对象来执行数据库操作,避免了直接编写SQL语句的复杂性。

  2.  提高开发效率‌:ORM框架提供了丰富的API和工具,使得数据库操作更加便捷和高效。

  3.  增强代码的可维护性‌:通过对象关系映射,代码与数据库的耦合度降低,便于维护和升级。

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

IsIgnore=true表示 ORM 所有操作不处理这列

一般用于数据库没有这一列

ORM 非数据库列加上该特性(配置导航查询自动IsIgnore=true)

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(it=>it 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,ConfigureExternalServices=new 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.IfTable<Order>().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.IfTable<Custom>().UpdateProperty(it => it.id, it =>{it.IsIdentity = true;it.IsPrimarykey = true;}).UpdateProperty(it => it.Text, it => {it.DataType= StaticConfig.CodeFirst_BigString;//支持多库的MaxString用法})//好处就是配置导航方便,和针对指定表设置会方便些//可以结合全局逻辑一起使用,如果下面逻辑和上面有冲突,下面的会覆盖上面的//统一设置 nullable等于isnullable=trueif(p.IsPrimaryKey==false&&new 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!;//这样写
}

http://www.yidumall.com/news/44985.html

相关文章:

  • 公司要建设网站需要那些程序网络优化包括
  • 用qq邮箱做网站如何进行网站性能优化?
  • 沧州网站推广优化商集客电话seo好找工作吗
  • 17网站一起做网店池尾商圈商品推广
  • 做游戏门户网站要注意什么意思公司网站制作
  • 安宁市建设厅网站百度搜索引擎广告投放
  • 萝岗网站建设制作sem是什么意思职业
  • 怎么将网站权重提上去全媒体运营师培训费用
  • 网站建设新手如何自己做网站互联网线上推广
  • 电商网站建设简单代码网页网络热词
  • 做网站三年3万块钱河南郑州最新事件
  • 上海 餐饮网站建设西部数码域名注册
  • 北京网站建设zhidaweb网络营销与直播电商专业学什么
  • 武安做网站石家庄网站建设就找
  • 自己做网站 需要会什么上海有什么seo公司
  • 电子商务企业网站设计怎么让客户主动找你
  • 云南省住房与城乡建设厅网站可以全部免费观看的软件
  • 本地网站搭建磁力搜索
  • 网站建设总结与关键词seo公司
  • 义乌做网站多少钱抖音账号权重查询
  • 西安优秀的集团门户网站建设服务商济南优化网站关键词
  • 贵阳做网站 优帮云品牌宣传推广方案
  • vs做网站三亚百度推广公司
  • 深圳公司网站推广网络推广方式
  • 怀化网站建设河北电子商务seo
  • 网站首页 flash大连seo建站
  • seo点击排名软件营销工具企业网站seo服务
  • 舟山网站建设百度推广收费
  • 南昌做公司网站百度竞价点击价格公式
  • 新变更营业执照注册号查了发现之前有备案过网站了seo咨询推广