Asp.net core中Migration工具使用的交流分享
2016-08-07 09:01
260 查看
一、文章参数
二、开篇碎语
三、主要内容
四、篇后总结
一、文章参数
开发工具:visual studio 2015 community update 3 + .net core tools(preview2) + sqlserver2012 express
开发环境:
win10(版本14393)+ .net core(版本 1.0.0-preview2-003121)
项目名称:
AirMusic
项目模板:
Asp.net core WebApi(这里不用mvc模板,是因为mvc模板初始的内容太多,这里用不到)
AirMusic源码地址:
https://github.com/boomyao/Blogs
二、开篇碎语
记得去年第一次做项目,用了asp.net mvc5,因此也第一接触了EntityFramework(版本是EF6)。 现在打算用asp.net core来完成一个项目,air music是学习asp.net core时新建的demo项目,以后学习core中遇到的一些问题一般会从这个项目产生,今天这篇文章是在migration初始化数据库时发生的一些问题。三、主要内容
1、初始化的实体模型
migrationBuilder.CreateTable( name: "ArtistSongs", columns: table => new { ArtistId = table.Column<int>(nullable: false), SongId = table.Column<int>(nullable: false), //ArtistId1 = table.Column<int>(nullable: true) }, constraints: table => { table.PrimaryKey("PK_ArtistSongs", x => new { x.ArtistId, x.SongId }); table.ForeignKey( name: "FK_ArtistSongs_Artists_ArtistId", column: x => x.ArtistId, principalTable: "Artists", principalColumn: "Id", onDelete: ReferentialAction.Cascade); //table.ForeignKey( // name: "FK_ArtistSongs_Artists_ArtistId1", // column: x => x.ArtistId1, // principalTable: "Artists", // principalColumn: "Id", // onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_ArtistSongs_Songs_SongId", column: x => x.SongId, principalTable: "Songs", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "SongListSongs", columns: table => new { SongListId = table.Column<int>(nullable: false), SongId = table.Column<int>(nullable: false), //SongListId1 = table.Column<int>(nullable: true) }, constraints: table => { table.PrimaryKey("PK_SongListSongs", x => new { x.SongListId, x.SongId }); table.ForeignKey( name: "FK_SongListSongs_Songs_SongId", column: x => x.SongId, principalTable: "Songs", principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_SongListSongs_SongLists_SongListId", column: x => x.SongListId, principalTable: "SongLists", principalColumn: "Id", onDelete: ReferentialAction.Cascade); //table.ForeignKey( // name: "FK_SongListSongs_SongLists_SongListId1", // column: x => x.SongListId1, // principalTable: "SongLists", // principalColumn: "Id", // onDelete: ReferentialAction.Restrict); });
初次Migration中发生意外的代码(注释的那些)
自动添加了带artist1和songlist1的字段,我很希望知道的朋友告诉我解决的办法!!我实在不知道怎么让EF不自动添加这个多余的字段,所以我把那些多余的字段都注释掉后,才update-database到数据库:
song-artist[N:N] , song-songlist[N:N]
在ApplicationDbContext的OnModelCreating方法里,可以手动的配置数据库中的关系,像什么组合主键啦,组合外键啦等等各种约束,都可以 实现。特别是数据库中实体的关系配置(1:1,1:N,N:N),例如:用方法builder.HasOne().WithMany()就可以建立[1:N]的关系。AirMusic初次Migration中,我也手动的配置了一些关系:
var entityAS=builder.Entity<ArtistSong>(); entityAS.HasKey("ArtistId", "SongId"); entityAS.HasOne<Artist>() .WithMany() .HasForeignKey("ArtistId"); var entitySS = builder.Entity<SongListSong>(); entitySS.HasKey("SongListId", "SongId"); entitySS.HasOne<SongList>() .WithMany() .HasForeignKey("SongListId");
上面代码的作用是,ArtistId和SongId设为ArtistSong的组合主键,ArtistSong的ArtistId设为Artist的外键。entitySS的作用也大致相同。
四、篇后总结
第一次完整的写一篇博文,晚上去吃饭是电脑自动重启更新了,vscode里的代码都没保存,打开博客园文章管理发现什么都没了,难过的就去听歌睡觉了。第二天起来打算从新来过时,发现有一行“自动保存恢复”,那个感觉就和中了100块的彩票一样。希望有人看完这篇文章吧,新写手最需要的就是多给建议呀!谢谢
相关文章推荐
- Asp.net core中Migration工具使用的交流分享
- 分享.NET里面的使用小工具
- 分享:使用 Capistrano —— Rails应用快速部署工具
- Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享
- Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享
- Duang~Duang~Duang 还在使用jsfiddle和jsbin做在线前端代码展示和演示吗? 试试更强大的在线代码分享工具吧!
- Softice使用指南(异域の蜗牛注:经典的调试工具,几乎能调试所有程序.感谢ddcrack让我看了这么好的文章.分享)
- Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享
- 分享自己使用的在线UML画图工具
- 微软SQL Server第三方工具使用分享
- J哥---------分享好东西:android抓包工具fiddler使用介绍 抓取 手机APP 中资源。
- 【分享】这些年,我使用的一些工具[非开发类]
- VDI负载测试工具使用分享:Login VSI简介
- 分享:适合新手使用的UML建模工具
- 分享自己使用的在线UML画图工具
- Mysql自带profiling性能分析工具使用分享
- 新手分享自己使用的在线UML画图工具
- Android开发之使用Handler封装下载图片工具类(源代码分享)
- gdb调试工具使用方法分享
- 分享好东西:android抓包工具fiddler使用介绍 抓取 手机APP 中资源。