当前位置:网站首页>EF core learning notes: one to many relationship configuration
EF core learning notes: one to many relationship configuration
2022-07-18 23:12:00 【Director Dong】
Recently migrated projects . It is found that the table structure is a one to many relationship . The positioning application form corresponds to multiple mold making . I am not familiar with this relationship , Dare not change . Now fill in the foundation .
Relationships between database tables : one-on-one 、 One to many 、 Many to many ( Need a third-party form to record ).
stay EF core in :
1. Relationship Attribute in entity class ;
2. FluentAPI Relationship configuration ;
3. Use relational operations .



1、 Migration generates database tables .
2、 Write code to test data insertion .
3、 There is no need to be explicit Comment Object's Article Attribute assignment ( There will be no error in the current assignment ), There is no need to explicitly create new Comment Objects of type are added to DbContext in .EF Core Meeting “ Climb along the pole ”.
Article a1 = new Article();
a1.Title = " Microsoft released .NET 6 The first preview of the big version ";
a1.Content = " tiny xxxx";
Comment c1 = new Comment() { Message=" Support "};
Comment c2 = new Comment() { Message = " Microsoft is awesome " };
Comment c3 = new Comment() { Message = " Leave a message/comment before it becomes a top/hot tweet " };
a1.Comments.Add(c1);
a1.Comments.Add(c2);
a1.Comments.Add(c3);
using (TestDbContext ctx = new TestDbContext())
{
ctx.Articles.Add(a1);
// ctx.Comments.Add(c1) There is no need to operate deliberately comment Watch
await ctx.SaveChangesAsync();
}
Personal understanding :
One to many configuration , You just need to operate article surface , Instead of deliberately operating comment surface .
let me put it another way : Just operate parent object .EF core It will help you operate automatically Sub object
Get relational data Use Include( ) Method
Article a = ctx.Articles.Include(a=>a.Comments).Single(a=>a.Id==1);
Console.WriteLine(a.Title);
foreach(Comment c in a.Comments)
{
Console.WriteLine(c.Id+":"+c.Message);
}
// Include It's defined in Microsoft.EntityFrameworkCore In the namespace .
// Take a look at the generated SQL sentence
边栏推荐
- Database design pattern of multi tenant SaaS
- 面试官:说一下你工作中发现的最有价值的bug
- 2022 soft test network administrator preparation guide
- The position of the lines drawn by canvas is disordered
- ImportError: cannot import name ‘Imputer‘ from ‘sklearn.preprocessing‘
- SQL Server locks nolock, updlock, holdlock, readpass
- Concept of Lun
- 416. Partition equal sum subset · knapsack problem · dynamic programming
- Original Rexroth proportional valve 4wrba10w64-2x/g24n9z4/m
- Openstack related blog
猜你喜欢

3D point cloud course (II) -- nearest neighbor problem

Program analysis and Optimization - 11 multi branch analysis

Beijing Hualian BHG mall continues to make efforts, and BHG day leads the new upsurge of urban consumption

Solution to Chinese garbled code in response results of burpsuite tool

【远程桌面】rustdesk开源的远程桌面,TeamViewer 和向日葵的替代品

T-infinite Road

20220714给AIO-3568J适配OpenHarmony-v3.1-beta(编译Buildroot)

Is there any prospect for individuals to do overseas social media? Any good suggestions?

C # network application programming, Experiment 5: data flow exercise

Interviewer: tell me about the most valuable bug you found in your work
随机推荐
Reflection and experience on problems in performance testing
No cl.exe solution found
PMP practice once a day | don't get lost in the exam -7.16
Multithreaded application
关于产品 | 要怎么进行产品规划?
11(2).结构体的存储方式,结构体变量和结构体变量指针作为函数参数传递的问题,指针的优点
Gao fushuai in unit testing, pytest framework (end) test report
ImportError: cannot import name ‘Imputer‘ from ‘sklearn.preprocessing‘
10 minutes to customize the pedestrian analysis system, detection and tracking, behavior recognition, human attributes all in one
leetcode--242. Valid alphabetic ectopic words
【决策树】使用决策树进行乳腺癌的诊断
NPM common commands use commands to delete nodes_ Modules package
Three scenarios take you to unit testing
私域运营很火,私域运营是否适合所有企业?
Nature Aging | 激活FOXM1基因人类寿命或翻倍
3大场景带你了解单元测试
[image editing software] FastStone Photo Resizer supports batch conversion and batch renaming
【C语言】strlen函数的讲解和模拟实现
BurpSuite工具响应结果中文乱码解决方法
[paddleseg source code reading] about the trivial matter that the paddleseg model returns a list