使用字符串作为主键 Entity Framework Code First
Using a string for an Primary Key Entity Framework Code First
我看过这个参考:
其中详细说明了我遇到的基本相同的问题。
运行更新数据库命令
导致以下错误:
'标识列 'FormId' 的数据类型必须为 int、bigint、smallint、tinyint 或 decimal 或 numeric,小数位数为 0,未加密且限制为不可为 null。'
我按照上面的参考说明添加了属性,并确保没有 Identity 属性与我正在尝试使用的数据库中的该列相关联,但在进行这些更改后,我仍然得到完全相同的错误消息.
听到我遗漏了什么,我真的很抓狂。我无法从搜索中找到任何其他修复程序。将不胜感激。
public class Form
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string FormId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public string Url { get; set; }
}
public class UserForm
{
public string UserFormId { get; set; }
public bool IsComplete { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public Form Form { get; set; }
}
尝试添加显式外键。
类似于:
public class UserForm
{
public string UserFormId { get; set; }
public bool IsComplete { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public string FormId {get;set;}
[ForeignKey("FormId")]
public Form Form { get; set; }
}
我尝试创建两个与您的模型相对应的表,字符串 FormId
并且一切正常。我建议您不要第一次创建表,而是将 FormId
从以前的类型(我认为是 int
类型)更改为新的 string
类型。如果是这样,在您的迁移中,您可以在 FormId
列中看到 AlterColumn
,它试图将 FormId
转换为新类型,但这是不可能的,因为它们不兼容。相反 AlterColumn
使用 DropColumn
和 AddColumn
组合。看一下非常相似(正如我假设的那样),其中试图将 PK 从 int
更改为 Guid
类型。
我看过这个参考:
其中详细说明了我遇到的基本相同的问题。 运行更新数据库命令 导致以下错误:
'标识列 'FormId' 的数据类型必须为 int、bigint、smallint、tinyint 或 decimal 或 numeric,小数位数为 0,未加密且限制为不可为 null。'
我按照上面的参考说明添加了属性,并确保没有 Identity 属性与我正在尝试使用的数据库中的该列相关联,但在进行这些更改后,我仍然得到完全相同的错误消息.
听到我遗漏了什么,我真的很抓狂。我无法从搜索中找到任何其他修复程序。将不胜感激。
public class Form
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string FormId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public string Url { get; set; }
}
public class UserForm
{
public string UserFormId { get; set; }
public bool IsComplete { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public Form Form { get; set; }
}
尝试添加显式外键。
类似于:
public class UserForm
{
public string UserFormId { get; set; }
public bool IsComplete { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public string FormId {get;set;}
[ForeignKey("FormId")]
public Form Form { get; set; }
}
我尝试创建两个与您的模型相对应的表,字符串 FormId
并且一切正常。我建议您不要第一次创建表,而是将 FormId
从以前的类型(我认为是 int
类型)更改为新的 string
类型。如果是这样,在您的迁移中,您可以在 FormId
列中看到 AlterColumn
,它试图将 FormId
转换为新类型,但这是不可能的,因为它们不兼容。相反 AlterColumn
使用 DropColumn
和 AddColumn
组合。看一下非常相似(正如我假设的那样)int
更改为 Guid
类型。