C# 如何查询具有子表集合的视图?

C# 如何查询具有子表集合的视图?,c#,ef-core-3.1,C#,Ef Core 3.1,我想查询EF Core 3.1中具有子表集合的视图。 以我的简化示例为例,有3个表: 公共类关系 { 公共长Id{get;set;} 公共字符串名{get;set;} 公共字符串LastName{get;set;} } 公共类发票 { 公共长Id{get;set;} 公共字符串引用{get;set;} 公共长关系ID{get;set;} 公共虚拟关系{get;set;} 公共虚拟ICollection发票行{get;set;}=new HashSet(); } 公共类发票行 { 公共长Id{ge

我想查询EF Core 3.1中具有子表集合的视图。 以我的简化示例为例,有3个表:

公共类关系
{
公共长Id{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
}
公共类发票
{
公共长Id{get;set;}
公共字符串引用{get;set;}
公共长关系ID{get;set;}
公共虚拟关系{get;set;}
公共虚拟ICollection发票行{get;set;}=new HashSet();
}
公共类发票行
{
公共长Id{get;set;}
公共十进制数{get;set;}
公共十进制价格{get;set;}
公共字符串ArticleReference{get;set;}
公共长发票ID{get;set;}
公共虚拟发票{get;set;}
}
我将SQL视图添加到迁移中:

protected override void Up(MigrationBuilder MigrationBuilder)
{
migrationBuilder.Sql(@)
创建或更改视图dbo.InvoiceOverview
作为
挑选
i、 身份证
,即参考文献
,r.FistName+“”+r.LastName作为名称
来自dbo.i
r.Id=i.RelationId“)上的内部连接关系r;
}
InvoiceView模型+配置

公共类发票视图
{
公共长Id{get;set;}
公共字符串引用{get;set;}
公共字符串名称{get;set;}
}
公共void配置(EntityTypeBuilder)
{
builder.HasKey(\u=>\ uq.Id);
承建商ToView(“发票概述”);
}
到目前为止还不错。以上所有操作都可以,但我希望能够查询发票行,如下所示:

Context.InvoiceOverview.AsNoTracking().AsQueryable().Where(=>
_.InvoiceLines.Select(invoiceLine=>invoiceLine.ArticleReference)
.Any(articleReference=>articleReference==“测试”).ToListSync();
我将InvoiceLine集合添加到InvoiceOverview模型并更新配置

公共类发票视图
{
公共长Id{get;set;}
公共字符串引用{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection发票行{get;set;}
}
公共void配置(EntityTypeBuilder)
{
builder.HasKey(\u=>\ uq.Id);
承建商ToView(“发票概述”);
builder.HasMany(\u=>\ u0.InvoiceLines)
.WithOne()
.HasForeignKey(\u=>\ u0.InvoiceId);
}
通过以上配置,我可以成功地运行查询。 我现在唯一的问题是,当我运行“添加迁移”时,他们想要创建一个外键。不可能为视图创建外键(并且该外键已经存在于视图中使用的表中)

protected override void Up(MigrationBuilder MigrationBuilder)
{
migrationBuilder.AddForeignKey(
名称:“FK\U发票行\U发票概述\U发票ID”,
表:“发票行”,
列:“发票ID”,
原则性:“发票概述”,
主栏:“Id”,
onDelete:引用。级联);
}
生成迁移脚本时是否可以显式忽略外键,或者是否应该以不同的方式编写配置?

我在中找到了一个解决方案

public void配置(EntityTypeBuilder)
{
builder.HasKey(\u=>\ uq.Id);
承建商ToView(“发票概述”);
builder.HasMany(\u=>\ u0.InvoiceLines)
.WithOne()
.HasForeignKey(\u=>\ u0.InvoiceId);
if(MigrationHelper.ismigOptionOperationExecuting())
{
忽略(x=>x.InvoiceLines);
}
}
公共静态类MigrationHelper
{
公共静态布尔值是迁移操作执行()
{
var commandLineArguments=Environment.GetCommandLineArgs();
字符串[]orderedMigrationArguments={“迁移”,“添加”};
对于(var i=0;i
public void Configure(EntityTypeBuilder<InvoiceOverview> builder)
{
    builder.HasKey(_ => _.Id);
    builder.ToView("InvoiceOverview");

    builder.HasMany(_ => _.InvoiceLines)
        .WithOne()
        .HasForeignKey(_ => _.InvoiceId);

    if (MigrationHelper.IsMigrationOperationExecuting())
    {
        builder.Ignore(x => x.InvoiceLines);
    }
}

public static class MigrationHelper
{
    public static bool IsMigrationOperationExecuting()
    {
        var commandLineArguments = Environment.GetCommandLineArgs();
        string[] orderedMigrationArguments = { "migrations", "add" };

        for (var i = 0; i <= commandLineArguments.Length - orderedMigrationArguments.Length; i++)
        {
            if (commandLineArguments.Skip(i).Take(orderedMigrationArguments.Length).SequenceEqual(orderedMigrationArguments))
                return true;
        }

        return false;
    }
}