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 SugarTest1 : Attribute
??? {
??????? public SugarTest1()
??????? {}
??????? public string TestTableName { get; set; }
??????? public SugarTest1(string name)
??????? {
??????????? this.TestTableName = name;
??????? }
??? }
??? /// <summary>
??? /// 给属性添加的特性
??? /// </summary>
??? [AttributeUsage(AttributeTargets.Property, Inherited = true)]
??? public class SugarTest2 : Attribute
??? {
??????? private string _ColumnName1;
??????? public string ColumnName1
??????? {
??????????? get { return _ColumnName1; }
??????????? set { _ColumnName1 = value; }
??????? }
??????? private bool _IsIgnore1;
??????? public bool IsIgnore1
??????? {
??????????? get { return _IsIgnore1; }
??????????? set { _IsIgnore1 = value; }
??????? }
??? }
2.2 创建带自定义特性的实体类
? [SugarTest1("TestClass")]
??? public class Test
??? {
??????? [SugarTest2(IsIgnore1 = true)]
??????? public int Id { get; set; }
??????? public string Name { get; set; }
??????? public string Age { get; set; }
??????? public string Address { get; set; }
??????? public string Area { get; set; }
??? }
2.3在创建SqlSugarClient对象中添加代码
ConfigureExternalServices = new ConfigureExternalServices()
??????????????? {
//设置列相关的属性
??????????????????? EntityService = (property, column) =>
??????????????????? {
??????????????????????? var attributes = property.GetCustomAttributes(true);//get all attributes
//如果不清楚具体走了哪个自定义特性,可以打印出来看看
??????????????????????? foreach (var attribute in attributes)
??????????????????????? {
??????????????????????????? if (attribute is SugarTest1)
??????????????????????????? {
??????????????????????????? }
??????????????????????????? if (attribute is SugarTest2)
??????????????????????????? {
??????????????????????????? }
??????????????????????? }
??????????????????????? if (attributes.Any(it => it is SugarTest2))// by attribute set primarykey
??????????????????????? {
??????????????????????????? column.DbColumnName = "小列";
??????????????????????? }
??????????????????? },
//设置表的属性
??????????????????? EntityNameService = (type, entity) =>
??????????????????? {
??????????????????????? var attributes = type.GetCustomAttributes(true);
??????????????????????? if (attributes.Any(it => it is SugarTest1))
??????????????????????? {
??????????????????????????? entity.DbTableName = "小表";
??????????????????????? }
??????????????????? }
??????????????? }
2.4添加创建表的代码,查看效果
断点打到2.3的位置上
//创建表
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Test));
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 | DecimalDigits | 精度;decimal(18,2),Length=19,DecimalDigits=2 | OldColumnName | 修改列名用,这样不会新增或删除列 | IsDisabledDelete | 禁止删除列 | IsDisabledUpdateAll | 禁止所有更新表的操作 | ColumnDataType | 字段类型 |
注意:
- EntityInfo:关于数据库表的一些属性
- EntityColumnInfo:关于数据库表字段的一些属性
文档参考:配置实体
|