【问题标题】:ef core - many to many relationshipef core - 多对多关系
【发布时间】:2020-05-01 13:30:27
【问题描述】:

我最近一直在尝试使用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();

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

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

【问题讨论】:

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


    【解决方案1】:

    您搞砸了关系映射,基本上将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)
    

    当然它们应该配对(LocationIdLocation)和(InstructorIdInstructor

    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)
    

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

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 2018-06-02
      • 2019-01-19
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 2021-03-30
      • 2017-09-08
      • 1970-01-01
      相关资源
      最近更新 更多