当前位置:网站首页>EF Core学习笔记:额外的外键属性 / 单项导航属性
EF Core学习笔记:额外的外键属性 / 单项导航属性
2022-07-20 04:22:00 【董厂长】
虽然一直在做定制化开发,但是不能总是食嗟来之食。很多东西还是自己必须要掌握的。
额外的外键属性
为什么需要外键属性
1、EF Core会在数据表中建外键列。
2、如果需要获取外键列的值,就需要做关联查询,效率低。(各种join)
3、需要一种不需要Join直接获取外键列的值的方式。
设置外键属性
1、在实体类中显式声明一个外键属性。
2、关系配置中通过HasForeignKey(c=>c.ArticleId)指定这个属性为外键。
3、除非必要,否则不用声明,因为会引入重复。
然后记得去配置!不然EFcore不知道这个外键是哪里来的。
复习一下 hasOne( )
单项导航属性
双向导航属性的麻烦:很多的表都会引用同一张表,如此,user表会建立很多反向的导航属性。
没必要
所以我们需要单项导航属性。
要求者 和 批准者 都指向user表
配置方法
不设置反向的属性,然后配置的时候WithMany()不设置参数即可。
单向属性如何反向获取数据
//再查询一下即可:
ctx.Leaves.Where(l => l.Requester == u)
对于主从结构的“一对多”表关系,一般是声明双向导航属性。
而对于其他的“一对多”表关系:如果表属于被很多表引用的基础表,则用单项导航属性,否则可以自由决定是否用双向导航属性。
考虑到有单项导航属性的可能,我们一般用HasOne().WithMany()
边栏推荐
- Batch梯度下降、随机梯度下降和mini-batch梯度下降
- 【MUDUO 日志系统2】Timestamp时间
- excel宏被禁用如何启用?excel无法启用宏的解决方法
- The difference between voice message and voice notification in okcc call center
- 【花雕体验】20 音乐可视化:ESP32_C3与WS2812B的系列尝试
- Solve gradient explosion and gradient disappearance
- The difference between break, continue and return
- Pytorch:model. Train () and model The usage and differences of Eval () and model Eval () and torch no_ The difference between grad()
- 检查日期是否在其他两个日期之间
- 基于ESP32的RGB点阵显示器
猜你喜欢
Binary tree OJ exercise
虚实相生,构建数智生活|HMS Core. Sparkle应用创新分论坛报名启动
Per job submission process of Apache Flink
结构体完结(柔性数组)
Explain IOU, giou, Diou, ciou, eiou and Diou NMS in detail
Solve gradient explosion and gradient disappearance
China's first white paper on the application of electronic signatures in the construction industry was released
元宇宙 3D 开荒场
详解FCOS《FCOS: Fully Convolutional One-Stage Object Detection》
软件界面和简单系统仿真
随机推荐
返回提供的日期所属的季度和年份
Processes and threads
Recommend a screenshot tool -snipaste
Excel如何进行多条件查找?Excel多条件查找函数的方法汇总
Difi: a go as you pay Wi Fi access system intensive reading notes (III)
The difference between voice message and voice notification in okcc call center
【ArcGIS微课1000例】0029:ArcGIS绘制平行线(构造平行公路)
集成 WebSocket 发送消息到客户端
网络中的参数量(param)和浮点计算量(FLOPs)的计算
Please stop using simpledateformat to format time. Datetimeformatter is better!
结构体完结(柔性数组)
Excel宏是什么?Excel宏的使用教程
Redis review
Why is the data found in the database partially lost
【MUDUO 】Channel通道
typora测试版过期无法正常使用
动态内存管理
Method of creating thread
Explain fcos: full revolutionary one stage object detection in detail
The first sentence of runaway it: a new colleague