【问题标题】:How to change datatype of foreign key in EF code-first如何在 EF 代码优先中更改外键的数据类型
【发布时间】:2017-01-22 09:06:36
【问题描述】:

我有两个模型

public class ContractType
{
    public Guid ContractTypeId { get; set; }
    public string Name { get; set; }
}

public class JobPost : BasePost
{
    public ContractType ContractType { get; set; }
    public Guid ContractTypeId { get; set; }    
}

如您所见,ContractTypeIdJobPost 表中的外键。

然后,我使用代码优先将它们更新到数据库。

但我只是决定让int 输入ContractTypeId 而不是Guid。所以我只是将ContractTypeId的数据类型更改为int并添加了迁移。两张表都没有数据。这就是我得到的

    public override void Up()
    {
        DropForeignKey("dbo.JobPosts", "ContractTypeId", "dbo.ContractTypes");
        DropIndex("dbo.JobPosts", new[] { "ContractTypeId" });
        DropPrimaryKey("dbo.ContractTypes");
        AlterColumn("dbo.ContractTypes", "ContractTypeId", c => c.Int(nullable: false, identity: true));
        AlterColumn("dbo.JobPosts", "ContractTypeId", c => c.Int(nullable: false));
        AddPrimaryKey("dbo.ContractTypes", "ContractTypeId");
        CreateIndex("dbo.JobPosts", "ContractTypeId");
        AddForeignKey("dbo.JobPosts", "ContractTypeId", "dbo.ContractTypes", "ContractTypeId", cascadeDelete: true);
    }

    public override void Down()
    {
        DropForeignKey("dbo.JobPosts", "ContractTypeId", "dbo.ContractTypes");
        DropIndex("dbo.JobPosts", new[] { "ContractTypeId" });
        DropPrimaryKey("dbo.ContractTypes");
        AlterColumn("dbo.JobPosts", "ContractTypeId", c => c.Guid(nullable: false));
        AlterColumn("dbo.ContractTypes", "ContractTypeId", c => c.Guid(nullable: false));
        AddPrimaryKey("dbo.ContractTypes", "ContractTypeId");
        CreateIndex("dbo.JobPosts", "ContractTypeId");
        AddForeignKey("dbo.JobPosts", "ContractTypeId", "dbo.ContractTypes", "ContractTypeId", cascadeDelete: true);
    }

但是,当我用update-database 更新数据库时,我得到一个错误:

错误号:206,状态:2,类:16
操作数类型冲突:uniqueidentifier 与 int 不兼容

我知道,由于我还没有为表填充任何数据,我可以简单地删除并重新创建模型并更新它们。

但我想知道如何解决这个问题或如何更改ContractTypeId 的数据类型。

谢谢大家

【问题讨论】:

  • 添加了我在本地 EF 6.1.3 上测试的答案,它在几次重试后就可以工作了 :) 顺便说一句,这对那些喜欢先在 EF 代码中执行此操作的人很有帮助。
  • @YashveerSingh 谢谢回复,我今晚试试!!
  • 好的没问题我希望它有效:)

标签: sql-server entity-framework code-first


【解决方案1】:

给你。因为您没有好的数据:) 棘手的是首先删除列并再次添加它们。因为我们不能将 uniquiueidentitfier 更改为 int 直接阅读一些提到的帖子 Change datatype of column to uniqueidentifier from bigint 。因此,我因此采用了迁移 :) 可能对喜欢先从 EF 代码进行迁移的其他人有所帮助。

public override void Up()
    {
        DropForeignKey("dbo.JobPosts", "ContractTypeId", "dbo.ContractTypes");
        DropIndex("dbo.JobPosts", new[] { "ContractTypeId" });
        DropPrimaryKey("dbo.ContractTypes");


        -- below line added manaully
        DropColumn("dbo.ContractTypes", "ContractTypeId");
        -- below line added manaully
        AddColumn("dbo.ContractTypes", "ContractTypeId", c => c.Int(nullable: false, identity: true));

        -- below line added manaully
        DropColumn("dbo.JobPosts", "ContractTypeId");

        -- below line added manaully
        AddColumn("dbo.JobPosts", "ContractTypeId", c => c.Int(nullable: false));


        AddPrimaryKey("dbo.ContractTypes", "ContractTypeId");
        CreateIndex("dbo.JobPosts", "ContractTypeId");
        AddForeignKey("dbo.JobPosts", "ContractTypeId", "dbo.ContractTypes", "ContractTypeId", cascadeDelete: true);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2018-02-26
    • 2018-05-31
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多