Entity Framework Core Relationship的学习笔记
说明
此例筛选了感兴趣及常用部分
参考文献
https://docs.microsoft.com/en-us/ef/core/modeling/relationships
One to Many
Many to Many
新增一个中间类,再转换成One to Many及One to Many的形式
class MyContext : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PostTag>()
.HasKey(t => new { t.PostId, t.TagId }); modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Post)
.WithMany(p => p.PostTags)
.HasForeignKey(pt => pt.PostId); modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Tag)
.WithMany(t => t.PostTags)
.HasForeignKey(pt => pt.TagId);
}
} public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; } public List<PostTag> PostTags { get; set; }
} public class Tag
{
public string TagId { get; set; } public List<PostTag> PostTags { get; set; }
} public class PostTag
{
public int PostId { get; set; }
public Post Post { get; set; } public string TagId { get; set; }
public Tag Tag { get; set; }
}
One to One(One to Zero)
class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<BlogImage> BlogImages { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.HasOne(p => p.BlogImage)
.WithOne(i => i.Blog)
.HasForeignKey<BlogImage>(b => b.BlogForeignKey);
}
} public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; } public BlogImage BlogImage { get; set; }
} public class BlogImage
{
public int BlogImageId { get; set; }
public byte[] Image { get; set; }
public string Caption { get; set; } public int BlogForeignKey { get; set; }
public Blog Blog { get; set; }
}
使用Migraion
将Model模型放在单独的类库中
使用CLI commands
需要在此类库中安装Migration必须的Nuget包,编辑.csproj
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
MSSql Server
dotnet ef migrations add Initial -c SchoolContext -o Data/SqlServerMigrations -s ../RelationshipStudy dotnet ef database update -s ../RelationshipStudy dotnet ef migrations remove -c SchoolContext -s ../RelationshipStudy
Entity Framework Core Relationship的学习笔记的更多相关文章
- .NET 5学习笔记(10)——Entity Framework Core之切换SQLServer和SQLite
上一篇我们梳理了CodeFist的一般流程,本篇我们讨论如何在一套代码中,支持SQL Server和SQLite的切换.同时从本篇开始,我们从.NET Core 3.1 迁移到.NET 5.相信.NE ...
- .NET Core学习笔记(8)——Entity Framework Core之Database First
曾经我以为再也不会去弄啥Database First,然鹅我错了.这个世界上就是有啪啪打脸和真香的时候.当小伙伴拿着做好的DB表结构和SQL脚本递过来的时候,我知道我没法拒绝.望着他突起的肱二头肌和充 ...
- ASP.NET Core 入门笔记9,ASP.NET Core + Entity Framework Core 数据访问入门
一.前言 1.本教程主要内容 ASP.NET Core MVC 集成 EF Core 介绍&操作步骤 ASP.NET Core MVC 使用 EF Core + Linq to Entity ...
- 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移
不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 读取关系数据
Reading related data¶ 9 of 9 people found this helpful The Contoso University sample web application ...
- Working with Data » 使用Visual Studio开发ASP.NET Core MVC and Entity Framework Core初学者教程
原文地址:https://docs.asp.net/en/latest/data/ef-mvc/intro.html The Contoso University sample web applica ...
- ASP.NET Core Web开发学习笔记-1介绍篇
ASP.NET Core Web开发学习笔记-1介绍篇 给大家说声报歉,从2012年个人情感破裂的那一天,本人的51CTO,CnBlogs,Csdn,QQ,Weboo就再也没有更新过.踏实的生活(曾辞 ...
- Professional C# 6 and .NET Core 1.0 - 38 Entity Framework Core
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 38 Entity Framework ...
- Professional C# 6 and .NET Core 1.0 - Chapter 38 Entity Framework Core
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 38 Entity F ...
随机推荐
- 如何使用getattr运行单个函数
import sys def foo(): print("哈哈想不到吧") if __name__ == '__main__': getattr(sys.modules[__nam ...
- TCP输入 之 tcp_data_queue
tcp_data_queue作用为数据段的接收处理,其中分为多种情况: (1) 无数据,释放skb,返回: (2) 预期接收的数据段,a. 进行0窗口判断:b. 进程上下文,复制数据到用户空间:c. ...
- mysql5.7以上基本配置
MySQL表名区分大小写设置 关闭MySQL服务 在服务运行目录找到my.ini或者my.cnf文件 find / -name my.cnf 打开文件,找到[mysqld]在下面增加一行 lower_ ...
- 2、记录代码----Ajax
$.ajax({ url:'/content-engine/index.php/tracker/confirmSendEmail', async: false, //默认为true,同意异步传输 da ...
- vue使用laydate.js插件报错laydate.css: Invalid
在vue中使用laydate.js插件时可能会碰到laydate.css: Invalid这样子的一个报错 然后导致laydate日期控件无法使用. 这主要是因为laydate.js中引入的layda ...
- tp5中很牛皮的一句sql语句,三个条件(两个不确定条件,一个硬性条件)
$result = Db::table('xxxxxx') // 表名 ->alias('g') ->join('xxxxx_2 u','g.user_id = u.id') -> ...
- ssh 远程登录错误
错误信息: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: REMOTE HOST IDENTIFICATI ...
- Spring Boot Cli下载安装
本地下载地址: spring-boot-cli-2.1.8.RELEASE-bin.zip : https://pan.baidu.com/s/1GMyxj1PecsM4BG_hzoteVQ spr ...
- Android studio2 中的 SDK Manager的使用-------Android SDK 的安装与更新(Install missing platform(s) and sync project 编译错误解决)
最近在编写Android程序,其中有一个问题就是对旧应用的导入,此时往往你的Android SDK中并没有老版本的Android SDK, 此时往往会提示你出现错误 Install missing p ...
- Java 谷歌浏览器开发必备插件
1.谷歌访问助手 下载网址:http://www.ggfwzs.com/ 2.Json Viewer 格式化请求接口,返回Json数据格式,可以在浏览器展示 3.Restlet client 一种类似 ...