entity-framework - ef core - 多对多关系

标签 entity-framework asp.net-core .net-core entity-framework-core

我最近一直在尝试使用 ef core,但是 ef core 中的多对多关系有些令人困惑。

    public class Location
{
    public Guid Id { get; set; }
    public ICollection<LocationInstructor> LocationInstructors { get; set; } = new List<LocationInstructor>();
}

public class Instructor
{
    public Guid Id { get; set; }
    public ICollection<LocationInstructor> LocationInstructors { get; set; } = new List<LocationInstructor>();
}

public class LocationInstructor
{
    public Guid LocationId { get; set; }
    public Location Location { get; set; }
    public Guid InstructorId { get; set; }
    public Instructor Instructor { get; set; }
}

并在 dbcontext
protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<LocationInstructor>()
            .HasKey(bc => new { bc.LocationId, bc.InstructorId });
        modelBuilder.Entity<LocationInstructor>()
            .HasOne(bc => bc.Location)
            .WithMany(b => b.LocationInstructors)
            .HasForeignKey(bc => bc.InstructorId);
        modelBuilder.Entity<LocationInstructor>()
            .HasOne(bc => bc.Instructor)
            .WithMany(c => c.LocationInstructors)
            .HasForeignKey(bc => bc.LocationId);
    }

这是我尝试执行的操作
var instructors = new List<Instructor>
        {
            new Instructor(),new Instructor()
        };
        await applicationDbContext.Instructors.AddRangeAsync(instructors);


        Location location = new Location();

        foreach (var instructor in instructors)
        {
            location.LocationInstructors.Add(new LocationInstructor { Instructor= instructor, Location=location});
        }

        await applicationDbContext.Locations.AddAsync(location);
        await applicationDbContext.SaveChangesAsync();

当我运行这个操作时,我可以看到下面的截图
enter image description here

所以,我的问题是为什么 2 值不同?我在这里错过了什么吗?

最佳答案

你搞砸了关系映射,基本上是关联 InstructorIdLocationLocationIdInstructor :

modelBuilder.Entity<LocationInstructor>()
    .HasOne(bc => bc.Location) // (1)
    .WithMany(b => b.LocationInstructors)
    .HasForeignKey(bc => bc.InstructorId); // (1)

modelBuilder.Entity<LocationInstructor>()
    .HasOne(bc => bc.Instructor) // (2)
    .WithMany(c => c.LocationInstructors)
    .HasForeignKey(bc => bc.LocationId); // (2)

当然,它们应该配对 ( LocationId , Location ) 和 ( InstructorId , Instructor )
modelBuilder.Entity<LocationInstructor>()
    .HasOne(bc => bc.Location) // (1)
    .WithMany(b => b.LocationInstructors)
    .HasForeignKey(bc => bc.LocationId); // (1)

modelBuilder.Entity<LocationInstructor>()
    .HasOne(bc => bc.Instructor) // (2)
    .WithMany(c => c.LocationInstructors)
    .HasForeignKey(bc => bc.InstructorId); // (2)

其中 btw 是默认的常规映射,因此可以跳过这些(约定优于配置)。

关于entity-framework - ef core - 多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61543438/

相关文章:

c# - 如何更改超时查询

.net - 如何在 Entity Framework 中执行 SQL BETWEEN 命令

ASP.NET Core 错误 : The JSON value could not be converted to System. 字符串

asp.net-core - 如何读取 dotnet core 上的 Windows 环境变量?

asp.net - 无法在文本框中键入 double

entity-framework - 在 Entity Framework 中找出导致异常的确切实体

c# - 如何获得执行程序集位置?

c# - WEB .NET 5 Newtonsoft 中的序列化 - 最后 2 位数字反序列化不正确

c# - 在 .Net Core 中将 HTTP/JSON 转码为 gRPC

c# - HttpClient 解码编码的 Url?