当前位置:网站首页>EF Core: 一对一,多对多的配置
EF Core: 一对一,多对多的配置
2022-07-29 04:40:00 【董厂长】
一对一
在“一对多”的关系中,很显然是需要在“多”端有一个指向“一”端的列,因此除非我们需要显式的声明一个外键属性,否则EF Core会自动在“多”端的表中生成一个指向“一”端的外键列,不需要我们显式的声明一个外键属性。
对于一对一关系,由于双方是“平等”的关系,外键列可以建在任意一方,因此必须显式的在其中一个实体类中声明一个外键属性。
class Order
{
public long Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public Delivery Delivery { get; set;}
}class Delivery
{
public long Id { get; set; }
public string CompanyName { get; set; }
public String Number { get; set; }
public Order Order { get; set; }
public long OrderId { get; set; }
}
EFCore表关系的配置
builder
.HasOne<Delivery>(o => o.Delivery)
.WithOne(d => d.Order)
.HasForeignKey<Delivery>(d=>d.OrderId);
多对多
需要中间表
class Student
{
public long Id { get; set; }
public string Name { get; set; }
public List<Teacher> Teachers { get; set; } = new List<Teacher>();
}class Teacher
{
public long Id { get; set; }
public string Name { get; set; }
public List<Student> Students { get; set; } = new List<Student>();
}
配置文件中,多对多的关系配置可以放到任何一方的配置类中,这里把关系配置代码放到了Student类的配置中。
builder
.HasMany<Teacher>(s => s.Teachers)
.WithMany(t=>t.Students)
.UsingEntity(j=>j.ToTable("T_Students_Teachers"));
边栏推荐
- Makefile(make)常见规则(二)
- [C language] PTA 7-91 output leap year
- Delete blank pages in word documents
- 使用近场探头和电流探头进行EMI干扰排查
- Go面向并发的内存模型
- Basic operation of queue
- Command line interactive tools (latest version) inquirer practical tutorial
- Pytorch fixed random seed & recurrence model
- Pytoch automatic mixing accuracy (AMP) training
- mujoco和mujoco_py安装以及解决libXcursor.so.1:NO such dictionary
猜你喜欢

WebRTC实现简单音视频通话功能

ssm整合增删改查

redux快速上手

删除word文档中的空白页

What is the difference between field, variable and property
![Understand the Internet giant [the war between China and Taiwan] and the development thinking of China and Taiwan](/img/6c/f24407133663c0e19d6fa05c611341.png)
Understand the Internet giant [the war between China and Taiwan] and the development thinking of China and Taiwan

The third ACM program design competition of Wuhan University of Engineering

Update learning materials daily

用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案

Unity基础(3)—— unity中的各种坐标系
随机推荐
Corresponding order of 18 and 25coco data of openpose and joint points
[c language] use the reverse order output of the linked list (bidirectional linked list)
Build auto.js script development environment
Vscode one click compilation and debugging
Implementation of img responsive pictures (including the usage of srcset attribute and sizes attribute, and detailed explanation of device pixel ratio)
Recommendation system of online education
Oracle update and delete data
Pytoch automatic mixing accuracy (AMP) training
Classes and objects (III)
VScode 一键编译和调试
Ethernet of network
Realize the effect of univariate quadratic equation through JS. Enter the coefficients of a, B and C to calculate the values of X1 and x2
[C] PTA 6-8 finding the height of binary tree
redux快速上手
数据湖:分布式开源处理引擎Spark
MySQL - 深入解析MySQL索引数据结构
钉钉对话框文子转换成图片 不能复制粘贴到文档上
Command line interactive tools (latest version) inquirer practical tutorial
Coding questions encountered in the interview
On the use of pyscript (realizing office preview)