一、文章参数

  • 开发工具:

    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、初始化的实体模型

public class Song

{

public int Id { get; set; }

[Required]

public string SongName { get; set; }

public virtual ICollection<ArtistSong> Artists { get; set; }

public virtual ICollection<SongListSong> SongLists { get; set; }

}

//歌手

public class Artist

{

public int Id { get; set; }

[Required]

public string Name { get; set; }

public virtual ICollection<Album> Albums { get; set; }

public virtual ICollection<ArtistSong> Songs { get; set; }

}

//song with artist n:n table

public class ArtistSong

{

[Key]

public int ArtistId { get; set; }

[Key]

public int SongId { get; set; }

}

//专辑

public class Album

{

public int Id { get; set; }

[Required]

public string Title { get; set; }

public virtual ICollection<Song> Songs { get; set; }

}

//歌单

public class SongList

{

public int Id { get; set; }

[Required]

public string Title { get; set; }

public string Describe { get; set; }

public virtual ApplicationUser User { get; set; }

public virtual ICollection<SongListSong> Songs { get; set; }

}

// song with songlist n:n table

public class SongListSong

{

[Key]

public int SongListId { get; set; }

[Key]

public int SongId { get; set; }

}

Store Models

2、引用EFcoore相关的Nuget包

  • "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0"

    (依赖了好多ef重要组件)

  • "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"

    (引用了才可以试用migration)

  • "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0"

    (我觉得是ef连接sqlserver的必须组件)

还有就是必须在project.json里的tools节点中添加"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",不然输入命令”Add Migration“时就会报出下面这句错误。

"No parameterless constructor………“这句错误是因为ApplicationDbContext(数据库上下文类)没有写下面这个构造函数报的错误。

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}

3、配置数据库连接

Webapi模板已经创建好了appsetting.json文件,这个文件的作用和web.config是一样的

4、文章包袱:Migration过程中遇到的问题

在ApplicationDbContext重写的方法OnModelCreating中,有一行代码

base.OnModelCreating(builder)

当我把这行代码注释掉时,迁移过程中就会报出如下错误:

很明显,错误发生的原因是IdentityUser没有被映射到ApplicationDbContext,所以可以知道这句代码的作用,就是用来映射Identity的几个实体类的,没有这句,数据库中就不会自动生成Users、Roles……等Table了,一个让我很困惑的问题在进行第一次Migration时出现了。AirMusic的实体中,有这样一种关系:

但是神奇的事情发生了(我不想这样的):

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块的彩票一样。

希望有人看完这篇文章吧,新写手最需要的就是多给建议呀!谢谢

原文地址:http://www.cnblogs.com/boomyao/p/5745525.html


.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注

Asp.net core中Migration工具使用的交流分享相关推荐

  1. 如何简单的在 ASP.NET Core 中集成 JWT 认证?

    前情提要:ASP.NET Core 使用 JWT 搭建分布式无状态身份验证系统 文章超长预警(1万字以上),不想看全部实现过程的同学可以直接跳转到末尾查看成果或者一键安装相关的 nuget 包 自上一 ...

  2. 在 ASP.NET Core 中集成 Skywalking APM

    前言 大家好,今天给大家介绍一下如何在 ASP.NET Core 项目中集成 Skywalking,Skywalking 是 Apache 基金会下面的一个开源 APM 项目,有些同学可能会 APM ...

  3. ASP.NET Core 中的规约模式(Specification Pattern )——增强泛型仓储模式

    原文链接:https://codewithmukesh.com/blog/specification-pattern-in-aspnet-core/ 在本文中,我们将讨论在 ASP.NET Core ...

  4. Api网关Kong集成Consul做服务发现及在Asp.Net Core中的使用

     1622219047536 写在前面   Api网关我们之前是用 .netcore写的 Ocelot的,使用后并没有完全达到我们的预期,花了些时间了解后觉得kong可能是个更合适的选择. 简单说下 ...

  5. 如何在 ASP.Net Core 中使用 SignalR

    SignalR for ASP.Net Core 是 SignalR 的浴火重生版,允许你在 ASP.Net Core 中实现实时通讯,这里的 实时 意味着双方都能快速的感知对方发来的消息,比如:一旦 ...

  6. 如何在 ASP.Net Core 中使用 MiniProfiler

    web应用程序的性能相信是大家普遍关心的一个问题,也相信大家有很多工具可用来分析应用程序的性能并能够找到其中的瓶颈,MiniProfiler 就是这个领域中的一款产品,它是一款简单的,功能强大的web ...

  7. 如何在 ASP.NET Core 中 使用 功能开关

    .NET Core 中的 功能管理 (Feature Management) 包可用于实现 功能开关,什么意思呢?就是可以通过 功能开关 特性动态的改变应用程序的行为而不需要改变任何的业务逻辑代码,听 ...

  8. 如何在 ASP.NET Core 中使用 URL Rewriting 中间件

    URL rewriting 是根据预先配置好的一组规则去修改 request url,值得注意的是:URL Rewriting 的重写功能和 url 重定向 是两个概念,本篇我们就来讨论下如何在 AS ...

  9. 如何在 Asp.Net Core 中对请求进行限流

    译文链接:https://www.infoworld.com/article/3442946/how-to-implement-rate-limiting-in-aspnet-core.html 在应 ...

最新文章

  1. MySQL中char与varchar的区别
  2. 一个基于Spring Boot的API、RESTful API项目骨架
  3. 大数据2 Hadoop伪分布模式配置部署
  4. 拉格朗日插值--等距节点Python实现并计算误差
  5. 2021研发效能实践案例征集大赛
  6. 如何应对数据库CPU打满?最优解在这里...
  7. SpringBoot异常处理-SimpleMappingExceptionResolver
  8. P2486 [SDOI2011]染色
  9. Java 8:长期支持的堡垒
  10. python用牛顿迭代法求平方根_Python编程实现二分法和牛顿迭代法求平方根代码
  11. 【java基础知识】spring框架开发时,怎样解决mysql数据库中Timestamp到String的简单转换
  12. Educoder 机器学习 神经网络 第四关:使用pytorch搭建卷积神经网络识别手写数字
  13. 【有利可图网】字体设计:英文字体的气质和选用详解(二)
  14. 【Python爬虫】懂车帝_车型库页面
  15. 5wpa_supplicant程序 --详解
  16. 618的优惠券去哪了?
  17. 游戏研发学习路线(思维导图)
  18. C语言——将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
  19. 网站cc攻击的防御步骤
  20. 数据标准化的方法与作用

热门文章

  1. 使用chpasswd命令批量修改系统用户密码
  2. Base PyQt4, Simple Web APP Framwork
  3. Avalonia跨平台入门第十三篇之Expander控件
  4. 分库分表下极致的优化
  5. 【Blog.Core开源】网关自定义认证鉴权与传参
  6. .net6给winform带来的新功能
  7. 我的C#/.NET学习诀窍——LINQPad
  8. JWT 介绍 - Step by Step
  9. Dapr是如何简化微服务的开发和部署
  10. Dapr 已在塔架就位 将发射新一代微服务