EF Code First 中具有不同 PK 类型的多对多关系
Many- Many relationship in EF Code First with different PK types
我有 2 个实体 Course
和 Group
,它们相互关联。我正在使用代码优先迁移。当我 update-database -v
它抛出异常
我知道多对多关系会创建另一个 table GroupCourses
.
StackTrace : ALTER TABLE [dbo].[GroupCourses] ALTER COLUMN [Course_CourseId] [int] NOT NULL
Exception : Operand type clash: uniqueidentifier is incompatible with int
public class Course
{
[Key]
public int CourseId { get; set; }
public virtual ICollection<Group> Groups { get; set; }
}
public class Group
{
[Key]
public Guid groupId { get; set; }
public virtual ICollection<Course> Courses{ get; set; }
}
我认为这是由于两个实体中的 PK 类型不同所致。我对么?如果是的话,有什么办法可以克服这个问题吗?
提前致谢。
你cannot update a primary key with Entity Framework。
当您尝试更新时,我假设 PK CourseId 从 Guid 到 int,这就是导致问题的原因(不是关联 table 中的不同类型的 PK,这很好).
删除 table 或手动更改类型。
我有 2 个实体 Course
和 Group
,它们相互关联。我正在使用代码优先迁移。当我 update-database -v
它抛出异常
我知道多对多关系会创建另一个 table GroupCourses
.
StackTrace : ALTER TABLE [dbo].[GroupCourses] ALTER COLUMN [Course_CourseId] [int] NOT NULL
Exception : Operand type clash: uniqueidentifier is incompatible with int
public class Course
{
[Key]
public int CourseId { get; set; }
public virtual ICollection<Group> Groups { get; set; }
}
public class Group
{
[Key]
public Guid groupId { get; set; }
public virtual ICollection<Course> Courses{ get; set; }
}
我认为这是由于两个实体中的 PK 类型不同所致。我对么?如果是的话,有什么办法可以克服这个问题吗?
提前致谢。
你cannot update a primary key with Entity Framework。
当您尝试更新时,我假设 PK CourseId 从 Guid 到 int,这就是导致问题的原因(不是关联 table 中的不同类型的 PK,这很好).
删除 table 或手动更改类型。