我正在使用 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/