实体配置
1.实体使用自带特性#
对于CURD来说,只需要配置主键和自增列就行;类的名称和数据库表名不同时,也可设置
1.1 主键自增#
[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; }
}
//创建表语句
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Student));
1.2 多个主键#
public class Student
{
[SugarColumn(IsPrimaryKey = true)] //设置主键
public Guid Pk1{ get; set; }
[SugarColumn(IsPrimaryKey = true)] //设置主键
public Guid Pk2{ get; set; }
public string Name { get; set; }
}
1.3 无主键#
public class Student
{
public Guid Id{ get; set; }
public string Name { get; set; }
}
2.实体使用自定义特性#
2.1 创建特性的类#
/// <summary>
/// 自定义类(表)特性
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public class CustomeAttributeClass : Attribute
{
//有参构造
public CustomeAttributeClass(string str)
{
testClass = str + "Test";
}
public string testClass { get; set; }
}
/// <summary>
/// 自定义属性(列)特性
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class CustomeAttributeColumn : Attribute
{
public CustomeAttributeColumn()
{
}
private string testColumn;
public string TestColumn
{
get { return testColumn; }
set { testColumn = value; }
}
}
2.2 创建带自定义特性的实体类#
[CustomeAttributeClass("At")]
public class TestModel
{
public int Id { get; set; }
[CustomeAttributeColumn(TestColumn = "Ac")]
public string Name { get; set; }
public int Age { get; set; }
}
2.3在创建SqlSugarClient对象中添加代码#
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.MySql,
ConnectionString = "server = 127.0.0.1; Database = sugarlearn; Uid = root; Pwd = root; AllowLoadLocalInfile = true;",
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
//自定义特性
ConfigureExternalServices = new ConfigureExternalServices()
{
//属性(列)的处理
EntityService = (property, column) =>
{
var attributes = property.GetCustomAttributes(true);//get all attributes
if (attributes.Any(it => it is CustomeAttributeColumn))// by attribute set primarykey
{
column.DbColumnName = (attributes.First(it => it is CustomeAttributeColumn) as CustomeAttributeColumn).TestColumn;
}
},
//类(表)的处理
EntityNameService = (type, entity) =>
{
var attributes = type.GetCustomAttributes(true);
if (attributes.Any(it => it is CustomeAttributeClass))
{
entity.DbTableName = (attributes.First(it => it is CustomeAttributeClass) as CustomeAttributeClass).testClass;
}
}
}
});
2.4添加创建表的代码,查看效果#
static void Main(string[] args)
{
var db = Sugar.GetInstance();
//新增表
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(TestModel));
Console.ReadLine();
}
3.实体不使用特性#
创建对象的时候,根据规则,指定哪个字段主键,哪个字段自增;这样就不需要在实体添加特性
var db= new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.MySql,
ConnectionString = Config.ConnectionString,
sAutoCloseConnection = 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;
}
}
}
}
});
4.特性明细#
下面是CRUD用到的特性,不包含建表的属性
名称 | 描述 |
---|---|
IsIdentity | 自增列 |
IsPrimaryKey | 创建主键 |
ColumnName | 实体类属性和数据库列名不同时,设置数据库列名 |
IsIgnore | ORM不处理该列 [即忽略] |
IsOnlyIgnoreInsert | 插入操作时不处理该列 [插入时忽略] |
IsOnlyIgnoreUpdate | 更新操作时不处理该列 [更新时忽略] |
OracleSequenceName | 设置Oracle序列,设置后该列等同于自增列 |
ColumnDescription | 备注 |
Length | 长度 |
IsNullable | 是否可以为Null;默认False(允许为NULL) |
DecimalDigits | 精度;decimal(18,2),Length=19,DecimalDigits=2 |
OldColumnName | 修改列名用,这样不会新增或删除列 |
IsDisabledDelete | 禁止删除列 |
IsDisabledUpdateAll | 禁止所有更新表的操作 |
ColumnDataType | 字段类型 |
注意:
- EntityInfo:关于数据库表的一些属性
- EntityColumnInfo:关于数据库表字段的一些属性
作者:DotNeter-Hpf
出处:https://www.cnblogs.com/DotNeter-Hpf/p/16619585.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
客官,点个推荐再走可好
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律