update

28810
2020-04-20 08:36:37 +08:00
parent d59487e1c3
commit 0ad1012617

41
AOP.md

@@ -71,24 +71,41 @@ A 为了考虑一致性用法,全部封装在 ColumnAttribute 下,这样
FreeSql 提供 AOP 自定义特性功能,实现与多个 orm 共同拥有一套实体特性,可避免重复定义特性。
> v1.4.0+ 已自动识别 EFCore 实体特性 Key/Required/NotMapped/Table/Column
```csharp
fsql.Aop.ConfigEntity += (s, e) => {
var attr = e.EntityType.GetCustomAttributes(
typeof(System.ComponentModel.DataAnnotations.Schema.TableAttribute), false).FirstOrDefault()
as System.ComponentModel.DataAnnotations.Schema.TableAttribute;
if (attr != null)
e.ModifyResult.Name = attr.Name; //表名
//e.ModifyIndexResult.Add(new IndexAttribute("idx_xx", "Title")); //索引
var attr = e.EntityType.GetCustomAttributes(typeof(MyTableAttribute), false).FirstOrDefault() as MyTableAttribute;
if (attr != null)
e.ModifyResult.Name = attr.Name; //表名
};
fsql.Aop.ConfigEntityProperty += (s, e) => {
if (e.Property.GetCustomAttributes(
typeof(System.ComponentModel.DataAnnotations.KeyAttribute), false).Any())
e.ModifyResult.IsPrimary = true; //主键
var attr = e.Property.GetCustomAttributes(typeof(MyColumnAttribute), false).FirstOrDefault() as MyColumnAttribute;
if (attr != null)
e.ModifyResult.Name = attr.Name; //字段名
};
```
就这样FreeSql 的实体特性就可以和 EFCore 那样设定了。其他自增、乐观锁等,依葫芦画瓢便是。
[MyTable("xxx")]
class YourEntity {
[MyColumn("id")]
public int pkid { get; set; }
}
class MyTableAttribute : Attribute {
public string Name { get; }
public MyTableAttribute(string name)
{
this.Name = name;
}
}
class MyColumnAttribute : Attribute {
public string Name { get; }
public MyColumnAttribute(string name)
{
this.Name = name;
}
}
```
## 表达式拦截