比如项目内已经使用了其它 orm,如 efcore,这样意味着实体中可能存在 [Key],但它与 FreeSql [Column(IsPrimary = true] 不同。

Q: FreeSql 实体特性为啥这么别扭?

A: 为了考虑一致性用法,全部封装在 ColumnAttribute 下,这样用户使用起来,不用到处 using 或者 回忆特性应该用哪个名字,如自增 [Column(IsIdentity = true)] 即可。

FreeSql 提供 AOP 自定义特性功能,实现与多个 orm 共同拥有一套实体特性,可避免重复定义特性。

以下的示例代码,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;
};
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; }
}

就这样,FreeSql 的实体特性就可以和 EFCore 那样设定了。其他自增、乐观锁等,依葫芦画瓢便是。

优先级

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

系列文章导航

最新文章

  1. IOS 支付、性能调试、IPv6兼容支持等
  2. poj 3984
  3. USACO section1.2 Transformation
  4. LVM管理及扩容(转)
  5. Java .Net C++ RSA 加密
  6. Android 6.0 Permission权限与安全机制
  7. ASP.NET网页抓取数据
  8. JavaScript事件处理
  9. 解决:未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0
  10. (大数据工程师学习路径)第一步 Linux 基础入门----简单的文本处理
  11. win10 Internet Explorer 11 停止工作处理
  12. Java多线程之线程状态总结
  13. 开源后的.Net 如何选择使用
  14. python requests 正则爬虫
  15. V-rep学习笔记:切削
  16. Unity3D Shader 高斯模糊
  17. mysql插入一条记录时有自增id怎么办
  18. SQl server更新某阶段的匹配关系。
  19. BZOJ5418 NOI2018屠龙勇士(excrt)
  20. Hadoop自定义类型处理手机上网日志

热门文章

  1. Windows下的bat原来可以为我们做很多
  2. 谈谈我对Ext的认识,元芳,你怎么看
  3. Streaming+Sparksql使用sql实时分析 rabbitmq+mongodb+hive
  4. 100天搞定机器学习|Day16 通过内核技巧实现SVM
  5. 送礼物「JSOI 2015」RMQ+01分数规划
  6. 什么时候使用redis?什么时候使用memcache?
  7. 用 程序 解决 windows防火墙 的 弹窗 问题
  8. 【译】为什么要了解HTTP
  9. PyQt编写Python GUI程序,简易示例
  10. F#周报2019年第34期