Ef Core,多对多连接 table 具有不同数据类型的复合键
Ef Core, many-to-many connection table composite key with different datatypes
如何确保具有不同数据类型的两个属性的组合键? EF Core 抱怨;
从'GroupUsers.Groups'到'Group.GroupUsers'具有外键属性{'UserId' : string}的关系不能以主键{[=为目标28=] : int} 因为它不兼容。为此关系配置主键或一组兼容的外键属性。
我的表是多对多关系,主键的类型不同,如下所示
public class Group
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int GroupId { get; set; }
[Required]
public string GroupName { get; set; }
public ICollection<GroupUsers> GroupUsers { get; set; }
}
public class User
{
[Key]
public string UserId { get; set; }
public string Email { get; set; }
public ICollection<GroupUsers> GroupUsers { get; set; }
}
public class GroupUsers
{
public string UserId { get; set; }
public int GroupId { get; set; }
[ForeignKey("GroupId")]
public Group Groups { get; set; }
[ForeignKey("UserId")]
public User Users { get; set; }
}
这是我忘记展示的 Fluent 方面
modelBuilder.Entity<GroupUsers>().HasKey(gu => new { gu.GroupId, gu.UserId});
modelBuilder.Entity<GroupUsers>().HasOne(ub => ub.Users)
.WithMany(x => x.GroupUsers).HasForeignKey(h => h.GroupId)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<GroupUsers>().HasOne(ub => ub.Groups)
.WithMany(x => x.GroupUsers).HasForeignKey(h => h.UserId)
.OnDelete(DeleteBehavior.Cascade);
看了你流畅的配置,看来你的HasForeignKey
调用被切换了。
应该是:
modelBuilder.Entity<GroupUsers>().HasKey(gu => new { gu.GroupId, gu.UserId});
modelBuilder.Entity<GroupUsers>().HasOne(ub => ub.Users)
.WithMany(x => x.GroupUsers).HasForeignKey(h => h.UserId) // <-- change to this
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<GroupUsers>().HasOne(ub => ub.Groups)
.WithMany(x => x.GroupUsers).HasForeignKey(h => h.GroupId) // <-- change to this
.OnDelete(DeleteBehavior.Cascade);
如何确保具有不同数据类型的两个属性的组合键? EF Core 抱怨;
从'GroupUsers.Groups'到'Group.GroupUsers'具有外键属性{'UserId' : string}的关系不能以主键{[=为目标28=] : int} 因为它不兼容。为此关系配置主键或一组兼容的外键属性。
我的表是多对多关系,主键的类型不同,如下所示
public class Group
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int GroupId { get; set; }
[Required]
public string GroupName { get; set; }
public ICollection<GroupUsers> GroupUsers { get; set; }
}
public class User
{
[Key]
public string UserId { get; set; }
public string Email { get; set; }
public ICollection<GroupUsers> GroupUsers { get; set; }
}
public class GroupUsers
{
public string UserId { get; set; }
public int GroupId { get; set; }
[ForeignKey("GroupId")]
public Group Groups { get; set; }
[ForeignKey("UserId")]
public User Users { get; set; }
}
这是我忘记展示的 Fluent 方面
modelBuilder.Entity<GroupUsers>().HasKey(gu => new { gu.GroupId, gu.UserId});
modelBuilder.Entity<GroupUsers>().HasOne(ub => ub.Users)
.WithMany(x => x.GroupUsers).HasForeignKey(h => h.GroupId)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<GroupUsers>().HasOne(ub => ub.Groups)
.WithMany(x => x.GroupUsers).HasForeignKey(h => h.UserId)
.OnDelete(DeleteBehavior.Cascade);
看了你流畅的配置,看来你的HasForeignKey
调用被切换了。
应该是:
modelBuilder.Entity<GroupUsers>().HasKey(gu => new { gu.GroupId, gu.UserId});
modelBuilder.Entity<GroupUsers>().HasOne(ub => ub.Users)
.WithMany(x => x.GroupUsers).HasForeignKey(h => h.UserId) // <-- change to this
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<GroupUsers>().HasOne(ub => ub.Groups)
.WithMany(x => x.GroupUsers).HasForeignKey(h => h.GroupId) // <-- change to this
.OnDelete(DeleteBehavior.Cascade);