相关实体查询
Related Entity Query
我有一个包含这些表的数据库 - 例如组织、位置和组织位置。 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();
但这让我很困惑。
感谢您的帮助
我将从所有三个 类 中删除虚拟集合和对象,然后删除 OrganisationLocations table 中的 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...
}
我有一个包含这些表的数据库 - 例如组织、位置和组织位置。 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();
但这让我很困惑。
感谢您的帮助
我将从所有三个 类 中删除虚拟集合和对象,然后删除 OrganisationLocations table 中的 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...
}