问题描述
在使用实体框架核心添加外键后尝试添加迁移时出现上述错误。 我正在添加 FK
public class ApplicantDetail
{
[Key]
public int Id { get; set; }
[ForeignKey("GrantProgramFK")]
public GrantProgram GrantProgramId { get; set; }
--------
}
这个FK跟班级有关
public class GrantProgram
{
[Key]
public int Id { get; set; }
-----
}
感谢任何帮助。我尝试删除实体并再次运行迁移,但失败了。
完全错误:
无法从实体类型“EFDataAccessLibrary.Models.ApplicantDetail”中删除属性“GrantProgramId”,因为它正用于“EFDataAccessLibrary.Models.ApplicantDetail”的外键 {'GrantProgramId'} 中。在删除属性之前,必须删除或重新定义所有包含外键的内容
我找到了一个页面, click here,但这是什么意思?如何解决。
解决方法
我遇到了同样的问题。我通过删除外键中的“id”解决了这个问题:
public class ApplicantDetail
{
[Key]
public int Id { get; set; }
[ForeignKey("GrantProgramFK")]
public GrantProgram GrantProgram { get; set; }
--------
}
我还是不知道为什么。如果有人可以解释,或者给出一个解决方案,在里面留下“id”,请不要犹豫,告诉我们。
,您必须在迁移之前修复您的类:
public class ApplicantDetail
{
[Key]
public int Id { get; set; }
public int? GrantProgramId { get; set; }
[ForeignKey("GrantProgramId")]
[InverseProperty("ApplicantDetails")]
public GrantProgram GrantProgram{ get; set; }
--------
}
public class GrantProgram
{
[Key]
public int Id { get; set; }
[InverseProperty(nameof(ApplicantDetail.GrantProram))]
public virtual ICollection<ApplicantDetail> ApplicantDetails{ get; set; }
-----
}
,
由于 entityframework 自动使用任何以“ID 或 Id 或 id”结尾的标识符,请确保没有其他属性名称以“ID 或 Id 或 id”结尾