entityframework 核心 - 多对多 - 属性 表达式无效
entityframework core - many to many - The property expression is not valid
应用程序版本:
Asp 网络核心 1.1
EF: Microsoft.EntityFrameworkCore (1.1.1)
行:
_dbContext.MyTable1.Include(c => c.MyIntermediateTable).ThenInclude(k => k.Select(x => x.MyTable2)).ToList();
异常:
The property expression 'k => {from MyIntermediateTable x in k select
[x].MyTable2}' is not valid. The expression should represent a
property access: 't => t.MyProperty'. For more information on
including related data
我的实体
[Table("MyTable1")]
public class MyTable1
{
public MyTable1()
{
MyIntermediateTable = new List<MyIntermediateTable>();
}
[Column("MyPK1")]
public int MyPK1 { get; set; }
public string Name{ get; set; }
public virtual List<MyIntermediateTable> MyIntermediateTable{ get; set; }
}
[Table("MyIntermediateTable")]
public class MyIntermediateTable
{
public int MyPK1 { get; set; }
public int MyPK2 { get; set; }
public virtual MyTable1 MyTable1 { get; set; }
public virtual MyTable2 MyTable2 { get; set; }
}
[Table("MyTable2")]
public class MyTable2
{
public MyTable2()
{
MyIntermediateTable = new List<MyIntermediateTable>();
}
[Column("MyPK2")]
public int MyPK2 { get; set; }
public string Name{ get; set; }
public virtual List<MyIntermediateTable> MyIntermediateTable{ get; set; }
}
模型生成器(流畅 API)
modelBuilder.Entity<MyIntermediateTable>((item) =>
{
item.HasKey(p => new { p.MyPK1, p.MyPK2 });
item.HasOne(u => u.MyTable1).WithMany(u => u.MyIntermediateTable).HasForeignKey(u => u.MyPK1);
item.HasOne(u => u.MyTable2).WithMany(u => u.MyIntermediateTable).HasForeignKey(u => u.MyPK2);
});
modelBuilder.Entity<MyTable1>((item) =>
{
item.HasKey(p => p.MyPK1);
item.HasMany(u => u.MyIntermediateTable).WithOne(u => u.MyTable1).HasForeignKey(u => u.MyPK1);
});
modelBuilder.Entity<MyTable2>((item) =>
{
item.HasKey(p => p.MyPK2);
item.HasMany(u => u.MyIntermediateTable).WithOne(u => u.MyTable2).HasForeignKey(u => u.MyPK2);
});
Visual Studio中的智能感知在进行自动竞争时只会显示列表属性。
这是一个错误(或者在撰写本文时 Intellisense 无法处理的问题),当有两个重载与签名匹配时会发生:一个用于 T
,另一个用于 IEnumerable<T>
.
出现这种情况时,即使智能感知不会向您显示 MyProperty
,只需键入 .ThenInclude(m => m.MyProperty)
,忽略 IDE 向您显示的任何错误。
关闭方法括号后,错误将消失。
额外信息
此错误由 dotnet roslyn GitHub 存储库上的此 GitHub 问题 Completion missing members of lambda parameter in fault tolerance case 跟踪。
应用程序版本:
Asp 网络核心 1.1
EF: Microsoft.EntityFrameworkCore (1.1.1)
行:
_dbContext.MyTable1.Include(c => c.MyIntermediateTable).ThenInclude(k => k.Select(x => x.MyTable2)).ToList();
异常:
The property expression 'k => {from MyIntermediateTable x in k select [x].MyTable2}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data
我的实体
[Table("MyTable1")]
public class MyTable1
{
public MyTable1()
{
MyIntermediateTable = new List<MyIntermediateTable>();
}
[Column("MyPK1")]
public int MyPK1 { get; set; }
public string Name{ get; set; }
public virtual List<MyIntermediateTable> MyIntermediateTable{ get; set; }
}
[Table("MyIntermediateTable")]
public class MyIntermediateTable
{
public int MyPK1 { get; set; }
public int MyPK2 { get; set; }
public virtual MyTable1 MyTable1 { get; set; }
public virtual MyTable2 MyTable2 { get; set; }
}
[Table("MyTable2")]
public class MyTable2
{
public MyTable2()
{
MyIntermediateTable = new List<MyIntermediateTable>();
}
[Column("MyPK2")]
public int MyPK2 { get; set; }
public string Name{ get; set; }
public virtual List<MyIntermediateTable> MyIntermediateTable{ get; set; }
}
模型生成器(流畅 API)
modelBuilder.Entity<MyIntermediateTable>((item) =>
{
item.HasKey(p => new { p.MyPK1, p.MyPK2 });
item.HasOne(u => u.MyTable1).WithMany(u => u.MyIntermediateTable).HasForeignKey(u => u.MyPK1);
item.HasOne(u => u.MyTable2).WithMany(u => u.MyIntermediateTable).HasForeignKey(u => u.MyPK2);
});
modelBuilder.Entity<MyTable1>((item) =>
{
item.HasKey(p => p.MyPK1);
item.HasMany(u => u.MyIntermediateTable).WithOne(u => u.MyTable1).HasForeignKey(u => u.MyPK1);
});
modelBuilder.Entity<MyTable2>((item) =>
{
item.HasKey(p => p.MyPK2);
item.HasMany(u => u.MyIntermediateTable).WithOne(u => u.MyTable2).HasForeignKey(u => u.MyPK2);
});
Visual Studio中的智能感知在进行自动竞争时只会显示列表属性。
这是一个错误(或者在撰写本文时 Intellisense 无法处理的问题),当有两个重载与签名匹配时会发生:一个用于 T
,另一个用于 IEnumerable<T>
.
出现这种情况时,即使智能感知不会向您显示 MyProperty
,只需键入 .ThenInclude(m => m.MyProperty)
,忽略 IDE 向您显示的任何错误。
关闭方法括号后,错误将消失。
额外信息
此错误由 dotnet roslyn GitHub 存储库上的此 GitHub 问题 Completion missing members of lambda parameter in fault tolerance case 跟踪。