繁体   English   中英

在 ef 核心中以多对多关系直接访问相关数据(并跳过链接类)

[英]accessing related data directly (and skipping linking class) in many to many relation in ef core

假设我想要一个 class A和很多B和很多A 我可以通过创建 class AB ,将ICollection<AB> ABs字段添加到AB类,然后通过 class AABs属性访问B来实现这一点。 这有效。 但我想知道有没有办法直接从 class A访问相关的B数据,而不是通过ABs属性。

我会想出几种方法来做到这一点(我都没有设法开始工作):

  • public ICollection<B> Bs => this.ABs.Select(item => item.B).ToList(); ,但它没有,我有 null 异常,即使我确实在此上下文中包含ABsBs context.As.Include(item => item.ABs).ThenInclude(item => item.B); .

  • ef core的流利api(不知道怎么做)

using Microsoft.EntityFrameworkCore;
using System;
using Context;

namespace ConsoleApp1 {
  class Program
  {
    static void Main(string[] args)
    {
      using var context = new ZContext();
      context.Database.EnsureDeleted();
      context.Database.EnsureCreated();
      var A = context.As.Include(item => item.ABs).ThenInclude(item => item.B);
      foreach (var a in A) {
        Console.WriteLine(a.Bs);
      }

      return;
    }
  }
}

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Context
{
  public class IDed
  {
    #region Constructors
    public IDed()
    {
      ID = Guid.NewGuid();
    } 
    #endregion

    public Guid ID { get; set; }
  }
  public class A : IDed
  {
    public ICollection<AB> ABs { get; set; }
    public ICollection<B> Bs { 
      get {
        return ABs.Select(item => item.B).ToList();
      }
    }
  }
  public class B : IDed
  {
    public ICollection<AB> ABs { get; set; }
  }
  public class AB
  {
    #region Constructors
    public AB()
    {

    }
    public AB(A a, B b)
    {
      this.AID = a.ID;
      this.BID = b.ID;
    } 
    #endregion

    public A A { get; set; } public Guid AID { get; set; }
    public B B { get; set; } public Guid BID { get; set; }
  }
  public class ZContext : DbContext
  {
    public DbSet<A> As { get; set; }
    public DbSet<B> Bs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      optionsBuilder.UseSqlite("Data Source=111.db3");
    }

    protected override void OnModelCreating(ModelBuilder mb)
    {
      #region Keys
      mb.Entity<AB>().HasKey(item => new { item.AID, item.BID });
      #endregion
      #region Relations
      // ???
      #endregion
      var a1 = new A();
      var a2 = new A();
      mb.Entity<A>().HasData(a1, a2);

      var b1 = new B(); 
      var b2 = new B();
      mb.Entity<B>().HasData(b1, b2);
      mb.Entity<AB>().HasData(
        new AB(a1, b1),
        new AB(a1, b2),
        new AB(a2, b1),
        new AB(a2, b2)
      );
    }
  }
}

使用第一种方法,我得到System.ArgumentNullException: 'Value cannot be null. (Parameter 'source')' System.ArgumentNullException: 'Value cannot be null. (Parameter 'source')'错误,即使我IncludeThenInclude所有相同的字段。

我意识到AB class 必须以任何方式保持不变,但是这种配置是否可以实现,以及这样做的正确方法是什么。

该异常是由 EF 对默认映射约定的推断引起的。 也就是说,在构建实体 model 时,EF 遇到属性A.Bs并尝试为其查找数据库映射。 异常发生在那个非常早的时刻。 一旦你知道了,修复很简单:只需取消映射属性:

[NotMapped]
public ICollection<B> Bs
{
    get
    {
        return ABs.Select(item => item.B).ToList();
    }
}

或者

protected override void OnModelCreating(ModelBuilder mb)
{
    ...
    mb.Entity<A>().Ignore(a => a.Bs);

当然,即使没有例外,也应该取消映射该属性,因为您不希望它反映在数据库关联中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM