具有在 EF 6 中工作的两个联接的 LINQ 查询在 EF 7 中出错
本文关键字:EF LINQ 查询 两个 出错 工作 | 更新日期: 2023-09-27 18:36:46
我有一个在EF 6(代码优先)项目中工作的LINQ查询。现在,我已将代码迁移到 EF 7,此查询现在引发异常:参数异常:未为类型"X.Models.Domain.MadeChoice"定义属性"Int32 ID"
查询:
var madeChoices = from res in X.Instance.Residence
join room in X.Instance.Room on res.ID equals room.Residence.ID
join madeChoice in X.Instance.MadeChoice on room.ID equals madeChoice.Room.ID
where res.ID == residence.ID
select room.MadeChoices;
MadeChoice类:
public class MadeChoice
{
public virtual int ID { get; set; }
[Required]
public virtual ChoiceGroup Choicegroup { get; set; }
[Required]
public virtual Room Room { get; set; }
[Required]
public virtual Item Item { get; set; }
}
房间等级:
public class Room
{
public virtual int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual Residence Residence { get; set; }
public virtual RoomType RoomType { get; set; }
public virtual List<MadeChoice> MadeChoices { get; set; }
// Constructors:
public Room()
{
this.MadeChoices = new List<MadeChoice>();
}
}
住宅类:
public class Residence
{
public int ID { get; set; }
public string ApartmentNumber { get; set; }
public static IQueryable<List<MadeChoice>> GetMadeChoices(Residence residence)
{
var madeChoices = from res in X.Instance.Residence
join room in X.Instance.Room on res.ID equals room.Residence.ID
join madeChoice in X.Instance.MadeChoice on room.ID equals madeChoice.Room.ID
where res.ID == residence.ID
select room.MadeChoices;
System.Diagnostics.Debug.Write("MadeChoices.Count: ");
System.Diagnostics.Debug.WriteLine(madeChoices.Count());
foreach (var madechoice in madeChoices)
{
System.Diagnostics.Debug.Write("MadeChoice.Count: ");
System.Diagnostics.Debug.WriteLine(madechoice.Count());
}
return madeChoices;
}
// Navigational properties:
public virtual List<Room> Rooms { get; set; }
public virtual ResidenceType ResidenceType { get; set; }
public virtual List<Tenant> Tenants { get; set; }
public virtual List<PeriodResidenceDeadline> PeriodResidenceDeadline { get; set; }
// Constructors:
public Residence()
{
this.Rooms = new List<Room>();
this.Tenants = new List<Tenant>();
this.PeriodResidenceDeadline = new List<PeriodResidenceDeadline>();
}
}
最初,该 ID 不是虚拟的,但它不会影响错误。数据库看起来与 EF 6 中所示。关系是一对多的。我使用 EF 7.0.0-rc1-final。
有什么提示吗?
提前感谢,
彼得
正如EF团队所说,EF Core RC1不处理复杂的子类型(请参阅他们的路线图 此处 https://github.com/aspnet/EntityFramework/wiki/Roadmap#in-progress 查询)
对于查询,这将在 EF Core 1.0 RTM 版本中处理。
同时,您可以使用我在此处枚举的解决方案之一:EF Core 流利映射到内部对象属性(映射的问题相同)