首页 > 解决方案 > 相关实体查询

问题描述

我有一个包含这些表的数据库——例如组织、位置和组织位置。Organizations 保存有关公司的信息,Locations 保存有关办公室地址的信息,OrganisationLocations 保存有关公司及其地址的多对多关系信息。例如,A 公司可能位于 X 和 Y 位置,B 公司可能位于 Y 和 Z 位置。

这些是生成的类

public partial class Locations
{
    public Locations()
    {
        OrganisationLocations = new HashSet<OrganisationLocations>();
    }

    public long Id { get; set; }
    public string Description { get; set; }
    public string City { get; set; }
    public string Street { get; set; }
    public string PostCode { get; set; }
    public string Name { get; set; }

    public virtual ICollection<OrganisationLocations> OrganisationLocations { get; set; }
}
public partial class Organisations
{
    public Organisations()
    {
        OrganisationLocations = new HashSet<OrganisationLocations>();
    }

    public long Id { get; set; }
    public string Name { get; set; }
    public string Owner { get; set; }
    public string ContactName { get; set; }
    public string Email { get; set; }
    public string Telephone { get; set; }

    public virtual ICollection<OrganisationLocations> OrganisationLocations { get; set; }

}
public partial class OrganisationLocations
{
    public long Id { get; set; }
    public long OrganisationId { get; set; }
    public long LocationId { get; set; }

    public virtual Locations Location { get; set; }
    public virtual Organisations Organisation { get; set; }
    }

数据库上下文如下所示:

public class PfApiContext : DbContext
{

    public PfApiContext(DbContextOptions<PaymentAPIContext> options)
            : base(options)
    {
    }

    public DbSet<Locations> Locations { get; set; }
    public DbSet<Organisations> Organisations { get; set; }
    public virtual DbSet<OrganisationLocations> OrganisationLocations { get; set; }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<OrganisationLocations>(entity =>
        {

             entity.HasOne(d => d.Location)
                .WithMany(p => p.OrganisationLocations)
                .HasForeignKey(d => d.LocationId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_84m7dbl1gv1p6tg1wquwm8j5u");

            entity.HasOne(d => d.Organisation)
                .WithMany(p => p.OrganisationLocations)
                .HasForeignKey(d => d.OrganisationId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_r0mkkndb6c2tr9nl0rgjm068t");
        });

    }
}

例如,我如何获取位置 X 中所有公司的所有公司数据?在我查询这个的时候,我实际上知道 LocationID,所以 SQL 将是

SELECT  * FROM [dbo].[Organisations] WHERE Id IN (SELECT [OrganisationId] FROM [dbo].[OrganisationLocations] WHERE [LocationId] = 1)

这可能真的很简单,我只是很愚蠢,但我是 EFCore 和 LINQ 的新手,它的语法让我摸不着头脑。根据我能理解的位置名称选择位置

var locationUsers = await _pfApiContext.UserLocations.Where
    (o => o.LocationName == locationName).ToListAsync();

但这让我感到很困惑。

谢谢你的帮助

标签: code-firstef-core-2.0

解决方案


我将从所有三个类中删除虚拟集合和对象,然后删除 OrganisationLocations 表中的 Id 字段(假设它是主键)。OrganisationLocations 中的 OrganisationId 和 LocationId 字段应该是主键和外键,以使其成为 OrganisationLocations、Locations 和 Organisation 之间的多对多关系。然后,您可以使用 Linq 进行简单的联接查询以获取您要查找的信息。例如:

            var orgs = (from ol in PfApiContext.OrganisationLocations
                        inner join o in PfApiContext.Organisation on o.Id equals ol.OrganisationId
                        inner join l in PfApiContext.Locations on ol.LocationId equals l.Id
                        where ol.LocationId = 1
                        select new Organisation()
                        {
                            Id = o.Id,
                            Name = o.Name,
                            Owner = o.Ower,
                            etc...

                        }

推荐阅读