申请版本:
Asp Net Core 1.1
EF:Microsoft.EntityFrameworkCore(1.1.1)
行:
_dbContext.MyTable1.Include(c => c.MyIntermediateTable).ToList();
例外:
类型' System.Data.SqlClient.SqlException'的异常发生在 Microsoft.EntityFrameworkCore.dll但未在用户代码中处理
其他信息:列名称无效' MyPK1'。
列名称无效' MyPK1'。
列名称无效' MyPK2'。
列名称无效' MyPK1'。
我的实体
[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; }
}
模型构建器(Fluent API)
modelBuilder.Entity<MyIntermediateTable>((item) =>
{
item.HasKey(p => p.MyPK1);
item.HasKey(p => 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);
});