按相关集合进行筛选时出错

时间:2019-03-06 06:54:28

标签: c# linq entity-framework-core ef-core-2.2

我在Windows 10 x64中使用Entity Framework Core 2.2.2

我具有以下解决方案结构:

  • DbContext(C#中的.Net标准)
  • 模型(Vb.Net中的.Net标准)
  • ConsoleApp(Vb.Net中的.Net 4.6.1)

我有以下模型和上下文:

Public Class User
    Public Property Id As Integer
    Public Property Firstname As String
    Public Property Lastname As String
    Public Property WorkingAreas As ICollection(Of WorkingArea)
End Class

Public Class WorkingArea
    Public Property Id As Integer
    Public Property UserId As Integer
    Public Property User As User
    Public Property ClientId As Integer
    Public Property Client As Client
End Class

Public Class Client
    Public Property ClientId As Integer
    Public Property Name As String
End Class

public class LinqTestContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<WorkingArea> WorkingAreas { get; set; }
    public DbSet<Client> Clients { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=localhost;Database=LinqDbTest;MultipleActiveResultSets=true;Persist Security Info=True;Trusted_Connection=True;");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().HasMany(a => a.WorkingAreas).WithOne(b => b.User).HasForeignKey(c => c.UserId);

    }
}

现在是我的问题:

可以将用户分配到多个工作空间。 现在,我希望工作区中所有具有ClientId 1的用户。

但我得到一个例外:

  

System.InvalidOperationException:类型“ System.Boolean”中的“类型为“ System.Collections.Generic.IEnumerable`1 [Microsoft.EntityFrameworkCore.Storage.ValueBuffer]”的示例,可能无法正常运行。 des Vorgangsändernwürde。Wenn去世,他是beabsichtigt ist,überschreibenSie“ VisitUnary”和nehmen Sie entsprechendeÄnderungenvor,sodass das Neuschreiben zugelassen wird。“

我尝试翻译:

  

System.InvalidOperationException:“类型的子表达式的重写” System.Collections.Generic.IEnumerable`1 [Microsoft.EntityFrameworkCore.Storage.ValueBuffer]类型“ System.Boolean”是不允许的,因为这将改变操作的含义如果这是故意的,请覆盖VisitUnary并进行适当的更改以允许重写。“

这是我的尝试,但这些都不起作用:

Using ctx1 As New TestContext.LinqTestContext
            'Dim users = ctx.Users.Where(Function(wa) wa.WorkingAreas.Where(Function(a) a.ClientId = 1).Any).ToList
            'Dim users = ctx.Users.Where(Function(wa) wa.WorkingAreas.Select(Function(s) s.ClientId).Contains(1)).ToList
            'Dim users = ctx.Users.Where(Function(wa) wa.WorkingAreas.Any(Function(x) x.ClientId = 1)).ToList
            Dim users = ctx.Users.Where(Function(wa) wa.WorkingAreas.Any(Function(x) x.Client.ClientId = 1)).ToList
        End Using

非常感谢您的帮助 谢谢进阶

0 个答案:

没有答案