【问题标题】:uniqueidentifier is incompatible with bigint entity framework c# code firstuniqueidentifier 不兼容bigint实体框架c#代码优先
【发布时间】:2017-06-24 16:16:53
【问题描述】:

我的 EF 代码中首先有这个实体

 public class Equipment
    {
        public Int64 Id { set; get; }
        public string Name { set; get; }
        public string Model { set; get; }
        public string Factory { set; get; }
        public DateTime SubmitDateTime { set; get; }
        public GUID OrganizationId { set; get; }
    }

我改成

 public class Equipment
    {
        public Int64 Id { set; get; }
        public string Name { set; get; }
        public string Model { set; get; }
        public string Factory { set; get; }
        public DateTime SubmitDateTime { set; get; }
        public Int64 OrganizationId { set; get; }
    }

但运行后出现此错误:

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

Additional information: Operand type clash: uniqueidentifier is incompatible with bigint

这里是我的迁移方法:

 public class MigrationsConfiguration : DbMigrationsConfiguration<DataContext>
    {

        public MigrationsConfiguration()
        {
            this.AutomaticMigrationDataLossAllowed = true;
            this.AutomaticMigrationsEnabled = true;
        }



    }

我的 Db 中有很多数据。所以我无法删除它以再次生成它。那么我该如何解决这个错误?

【问题讨论】:

  • 您的组织表中有数据吗?你不能这样做,错误很明显。组织表具有 GUID 作为主键,设备中的组织 ID 正在引用它。现在您将其更改为 bigint,但它不知道该怎么做。
  • 任意更改的一种简单方法是从新数据库重新开始。让 EF 创建表,然后从旧数据库中加载它们。
  • 您需要首先更改 Organization 表中的 OrganizationId 列,用 bigint 填充它们,然后更新您的 Equipment 表,使其使用新值。您将需要暂存表格,删除外键等。
  • @DavidBrowne-Microsoft 正如我所说,我的数据库中有很多数据
  • 您对编写 SQL 脚本感到满意吗?

标签: c# entity-framework guid entity-framework-migrations int64


【解决方案1】:

通过三个步骤,我改变了它: 第一的 我向我的实体添加了另一列

public GUID OrganizationId { set; get; }
public int64 OrganizationId2 { set; get; }

秒 我删除了实体中的 guid 列

public int64 OrganizationId2 { set; get; }

最后一步 最后我重命名了我的新专栏

public int64 OrganizationId { set; get; }

【讨论】:

  • 这就是为什么我问你是否可以使用 SQL 脚本。我正要提出相同的建议,但还有一个步骤:您需要填充父表和子表中的列。你没有这样做,因为你有数据,这是非常重要的一步。
  • 这么务实的做法,没想到这么基本的解决方案哈哈。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-09-01
  • 2014-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-29
  • 2012-05-31
  • 2012-12-30
相关资源
最近更新 更多