c# - Entity Framework 迁移 - Alter Table 语句与外键约束冲突

标签 c# mysql asp.net-mvc entity-framework

我正在使用 EF 代码优先迁移,通过 C#/.NET 4.5 框架创建一个博客。

事情一直进展顺利,直到将第三个关系添加到我的主类中。

我有一个“故事”类(有点像博客的“帖子”),其中作者是登录的用户(在 Controller 中设置)、标题、一些内容、日期创建,以及故事的流派和类型。

public class Story
    {
        public int Id { get; set; }

        public string AuthorId { get; set; }
        public virtual ApplicationUser Author { get; set; }

        [Required]
        [StringLength(50)]
        public string Title { get; set; }

        [Required]
        [MinLength(100), MaxLength(5000)]
        public string Content { get; set; }

        [Required]
        public int GenreId { get; set; }
        public Genre Genre { get; set; }

        [Required]
        public int StoryTypeId { get; set; }
        public StoryType StoryType { get; set; }

        public DateTime CreatedAt { get; set; }
    }

我将故事类型添加为故事的属性。 StoryType 链接到 StoryType 模型:

public class StoryType
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

我确保将我的数据库集添加到我的应用程序数据库上下文中:

public DbSet<Genre> Genres { get; set; }
public DbSet<Story> Stories { get; set; }
public DbSet<StoryType> StoryTypes { get; set; }

我几乎遵循了创建故事和类型之间关系的相同步骤(效果很好)。在开始构建 StoryType Controller 之前,我进入 package-console 并运行:

add-migration

返回:

 public partial class CreateTypeTable : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.StoryTypes",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(),
                    })
                .PrimaryKey(t => t.Id);

            AddColumn("dbo.Stories", "StoryTypeId", c => c.Int(nullable: false));
            CreateIndex("dbo.Stories", "StoryTypeId");
            AddForeignKey("dbo.Stories", "StoryTypeId", "dbo.StoryTypes", "Id", cascadeDelete: true);
        }

        public override void Down()
        {
            DropForeignKey("dbo.Stories", "StoryTypeId", "dbo.StoryTypes");
            DropIndex("dbo.Stories", new[] { "StoryTypeId" });
            DropColumn("dbo.Stories", "StoryTypeId");
            DropTable("dbo.StoryTypes");
        }
    }

扫了一眼,没发现问题,就跑了:

update-database

在包控制台中。

返回:

Error Number:547,State:0,Class:16
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Stories_dbo.StoryTypes_StoryTypeId". The conflict occurred in database "aspnet-HiRatik.Stories-20180724043630", table "dbo.StoryTypes", column 'Id'.

我不确定这里出了什么问题。我对类型关系也做了同样的处理,结果很有效。我没看出两者有什么区别。

最佳答案

因为 Story 类中的 StoryTypeId 不接受 null,所以您需要使 StoryTypeId 可空:

public class Story
    {
        public int Id { get; set; }

        public string AuthorId { get; set; }
        public virtual ApplicationUser Author { get; set; }

        [Required]
        [StringLength(50)]
        public string Title { get; set; }

        [Required]
        [MinLength(100), MaxLength(5000)]
        public string Content { get; set; }

        [Required]
        public int GenreId { get; set; }
        public Genre Genre { get; set; }

        public int? StoryTypeId { get; set; }
        public StoryType StoryType { get; set; }

        public DateTime CreatedAt { get; set; }
    }

或者您首先创建表 StoryType 并向其中添加元素,然后添加具有默认值的 StoryTypeId:

公开课故事 { 公共(public) int Id { 得到;放; }

    public string AuthorId { get; set; }
    public virtual ApplicationUser Author { get; set; }

    [Required]
    [StringLength(50)]
    public string Title { get; set; }

    [Required]
    [MinLength(100), MaxLength(5000)]
    public string Content { get; set; }

    [Required]
    public int GenreId { get; set; }
    public Genre Genre { get; set; }

    [[DefaultValue(1)]]
    public int StoryTypeId { get; set; }
    public StoryType StoryType { get; set; }

    public DateTime CreatedAt { get; set; }
}

在这种情况下,您必须在创建 StoryType 并将 StoryTypeId 添加到 Story 类之后更新数据库

关于c# - Entity Framework 迁移 - Alter Table 语句与外键约束冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51526632/

相关文章:

C# MVC Entity Framework 只保存最后一行

c# - 将 GPS 解析为度数

c# - 实现IDataErrorInfo接口(interface)时 `public string this[string columnName]`是什么意思?

php - 为什么 mysql 中的查询有效,但 php 中的查询不起作用,这只是由于特定的列

asp.net-mvc - ASP.NET MVC 3 区域 - 无法使用自定义路由找到 View

jquery - 使用 MVC PartialView 运行 jquery 脚本 block

asp.net-mvc - 为 IIS 托管的 .SVC 文件配置 XML-RPC 行为?

C# - @if 内有未封闭的 HTML 元素时出现 System.Web.HttpParseException 错误

javascript - 如何使用从 MySQL 数据库获取的 JSP 代码打破日期时间格式并显示在单独的文本框中

php - 如何将2个不同表中的xml数据复制到mysql