Skip to content

自定义特性

28810 edited this page Apr 15, 2020 · 16 revisions

抛砖引玉,以下的示例代码,FreeSql 使用 EFCore 的实体特性:

fsql.CodeFirst.ConfigEntity<ModelAopConfigEntity>(a => a.Property(b => b.pkid).IsPrimary(true));

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")); //索引
};
fsql.Aop.ConfigEntityProperty += (s, e) => {
  if (e.Property.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.KeyAttribute), false).Any())
    e.ModifyResult.IsPrimary = true; //主键
};

[System.ComponentModel.DataAnnotations.Schema.Table("xxx")]
class ModelAopConfigEntity {
  [System.ComponentModel.DataAnnotations.Key]
  [Column(IsPrimary = false)]
  public int pkid { get; set; }
}

优先级

数据库特性 > 实体特性 > FluentApi(配置特性) > Aop(配置特性)

参考资料

Clone this wiki locally