mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-06 16:30:52 +08:00
update
41
AOP.md
41
AOP.md
@@ -71,24 +71,41 @@ A: 为了考虑一致性用法,全部封装在 ColumnAttribute 下,这样
|
|||||||
|
|
||||||
FreeSql 提供 AOP 自定义特性功能,实现与多个 orm 共同拥有一套实体特性,可避免重复定义特性。
|
FreeSql 提供 AOP 自定义特性功能,实现与多个 orm 共同拥有一套实体特性,可避免重复定义特性。
|
||||||
|
|
||||||
|
> v1.4.0+ 已自动识别 EFCore 实体特性 Key/Required/NotMapped/Table/Column
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
fsql.Aop.ConfigEntity += (s, e) => {
|
fsql.Aop.ConfigEntity += (s, e) => {
|
||||||
var attr = e.EntityType.GetCustomAttributes(
|
var attr = e.EntityType.GetCustomAttributes(typeof(MyTableAttribute), false).FirstOrDefault() as MyTableAttribute;
|
||||||
typeof(System.ComponentModel.DataAnnotations.Schema.TableAttribute), false).FirstOrDefault()
|
if (attr != null)
|
||||||
as System.ComponentModel.DataAnnotations.Schema.TableAttribute;
|
e.ModifyResult.Name = attr.Name; //表名
|
||||||
if (attr != null)
|
|
||||||
e.ModifyResult.Name = attr.Name; //表名
|
|
||||||
//e.ModifyIndexResult.Add(new IndexAttribute("idx_xx", "Title")); //索引
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fsql.Aop.ConfigEntityProperty += (s, e) => {
|
fsql.Aop.ConfigEntityProperty += (s, e) => {
|
||||||
if (e.Property.GetCustomAttributes(
|
var attr = e.Property.GetCustomAttributes(typeof(MyColumnAttribute), false).FirstOrDefault() as MyColumnAttribute;
|
||||||
typeof(System.ComponentModel.DataAnnotations.KeyAttribute), false).Any())
|
if (attr != null)
|
||||||
e.ModifyResult.IsPrimary = true; //主键
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## 表达式拦截
|
## 表达式拦截
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user