具有两个在EF 6中工作的连接的LINQ查询在EF 7中给出了错误

时间:2016-03-01 08:35:02

标签: c# entity-framework linq join

我有一个在EF 6(代码优先)项目中工作的LINQ查询。现在我已将代码迁移到EF 7,此查询现在抛出异常:ArgumentException:Property' Int32 ID'未定义类型' X.Models.Domain.MadeChoice'

查询:

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; }
}

The Room类:

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。

任何提示?

提前致谢,

彼得

1 个答案:

答案 0 :(得分:2)

正如EF团队所说,EF Core RC1无法处理复杂的子类型(请参阅此处的路线图https://github.com/aspnet/EntityFramework/wiki/Roadmap#in-progress查询)

对于查询,这将在EF Core 1.0 RTM版本中处理。

与此同时,您可以使用我在此列举的解决方案之一:EF Core fluent mapping to inner object properties(映射的问题相同)