Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ query with two joins that worked in EF 6 gives error in EF 7

I have a LINQ query that works in an EF 6 (Code First) project. Now I have migrated the code to EF 7, and this query now throws an exception: ArgumentException: Property 'Int32 ID' is not defined for type 'X.Models.Domain.MadeChoice'

The query:

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;

The MadeChoice class:

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 class:

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

The Residence class:

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

Originally, the ID was not virtual but it didn't affect the error. The database looks as in EF 6. The relationships are one-to-many. I use EF 7.0.0-rc1-final.

Any hints?

Thanks in advance,

Peter

like image 599
Peter Lindgren Avatar asked Mar 01 '16 08:03

Peter Lindgren


People also ask

Can we use joins in LINQ?

LINQ Join queries. As we know the JOIN clause is very useful when merging more than two table or object data into a single unit. It combines different source elements into one and also creates the relationship between them. Using the join, you can grab the data based on your conditions.

Which join is valid in LINQ?

Venn diagram for LINQ Joins The JOIN query operator compares the specified properties/keys of two collections for equality by using the EQUALS keyword. By default, all join queries written by the JOIN keyword are treated as equijoins.

How do I join multiple tables in EF core?

The LINQ join operator allows us to join multiple tables on one or more columns (multiple columns). By default, they perform the inner join of the tables. We also learn how to perform left joins in EF Core by using the join operator & DefaultIfEmpty method. Also left join with where clause.


1 Answers

As EF team said, EF Core RC1 doesn't handle complex sub-types (see their roadmap here https://github.com/aspnet/EntityFramework/wiki/Roadmap#in-progress for Query)

For querying, this will be handle in the EF Core 1.0 RTM version.

In the meantime, you can use one of the solution I enumerate here : EF Core fluent mapping to inner object properties (the problem is the same for mapping)

like image 100
cdie Avatar answered Oct 19 '22 12:10

cdie