【发布时间】: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