none
Possible Bug: Include not working properly.

    Question

  • Assume the following classes:

    public class Rolling{
        [Key]
        public int Id { get; set; }
        ...
        public virtual List<Itinerary> Itineraries { get; set; }
    }
    
    public class Itinerary{
        [Key]
        public int Id { get; set; }
        ...
        public int? VehicleId { get; set; }
        public int? TrailerId { get; set; }
        public virtual Rolling Vehicle { get; set; }
        public virtual Rolling Trailer { get; set; }
    }


    and the fluent API:

    modelBuilder.Entity<Rolling>()
                .HasMany(c => c.Itineraries)
                .WithOptional(c => c.Vehicle)
                .WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Rolling>()
                .HasMany(c => c.Itineraries)
                .WithOptional(c => c.Trailer)
                .WillCascadeOnDelete(false);

    when I run a query:

    var aRolling = mgr.Rolling.Include("Itineraries").First();

    var itineraries = mgr.Itineraries.Local;

    Include will only retrieve Itineraries that comply with the last fluent API definition. (i.e. Trailer)

    If I switch the order of the fluent API definitions, then Include will retrieve the Itineraries that have non-null Vehicles instead.

    I would expect to get all Itineraries regardless.

    Is this a bug?

    Thanks in advance.

    Thursday, September 19, 2013 7:13 PM

Answers

  • Hello,

    Welcome to this forum.

    >> Include will only retrieve Itineraries that comply with the last fluent API definition. (i.e. Trailer)

    As far as I know, it will be override by last setting.

    Like codes below:

    modelBuilder.Entity<Rolling>().HasMany(c => c.Itineraries).WithOptional(c => c.Vehicle).WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Rolling>().HasMany(c => c.Itineraries).WithOptional(c => c.Trailer).WillCascadeOnDelete(false);

    It is actually equal with codes:

    modelBuilder.Entity<Rolling>().HasMany(c => c.Itineraries).WithOptional(c => c.Trailer).WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Rolling>().HasMany(c => c.Itineraries).WithOptional(c => c.Vehicle).WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Rolling>().HasMany(c => c.Itineraries).WithOptional(c => c.Trailer).WillCascadeOnDelete(false);

    And I am confused about “expect to get all Itineraries regardless.”

    Could you please share how the result you want?

    Thanks &Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, September 20, 2013 8:22 AM
    Moderator

All replies

  • Hello,

    Welcome to this forum.

    >> Include will only retrieve Itineraries that comply with the last fluent API definition. (i.e. Trailer)

    As far as I know, it will be override by last setting.

    Like codes below:

    modelBuilder.Entity<Rolling>().HasMany(c => c.Itineraries).WithOptional(c => c.Vehicle).WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Rolling>().HasMany(c => c.Itineraries).WithOptional(c => c.Trailer).WillCascadeOnDelete(false);

    It is actually equal with codes:

    modelBuilder.Entity<Rolling>().HasMany(c => c.Itineraries).WithOptional(c => c.Trailer).WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Rolling>().HasMany(c => c.Itineraries).WithOptional(c => c.Vehicle).WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Rolling>().HasMany(c => c.Itineraries).WithOptional(c => c.Trailer).WillCascadeOnDelete(false);

    And I am confused about “expect to get all Itineraries regardless.”

    Could you please share how the result you want?

    Thanks &Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, September 20, 2013 8:22 AM
    Moderator
  • Could you please share how the result you want?

    Hi, if possible, I need to get all the itineraries of a specific 'Rolling' entity regardless of wheter the link comes from the Vehicle of Trailer properties.

    Hope this is clear.


    Friday, September 20, 2013 9:51 AM
  • Hi,

    Any idea how to get all the itineraries as requested? As explained by sbelini, when the fluent API declaration are inversed, the result is not the same. If this is not an Entity Framework bug, can you please show us how to proceed? We need to retrieve all the itineraries. 

    Thanks.

    Sunday, September 22, 2013 2:10 PM
  • So I guess that if the id in rolling table is 1, and then you want to get all itineraries both TrailerId =1 and VehicleId =1.

    If it is, my suggestion is to do a query to get all the itineraries and it will be easy.

    Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 23, 2013 7:08 AM
    Moderator
  • So I guess that if the id in rolling table is 1, and then you want to get all itineraries both TrailerId =1 and VehicleId =1.

    If it is, my suggestion is to do a query to get all the itineraries and it will be easy.

    Performing a query in my situation is not the best solution because it means that I have a main querry to get a list of rollings and then for each rolling I have to perform a querry to get all itineraries.

    I know this is possible to get all the itineraries of a specific 'Rolling' entity when there is only one linked property (only Vehicle or only Trailer).

    So do you mean this is not possible to get all the itineraries of a specific 'Rolling' entity regardless of wheter the link comes from the Vehicle of Trailer properties?


      
    Monday, September 23, 2013 8:49 AM
  • Hi,

    From my opinion, I think it is impossible, because I can hardly find a way to get all the all the itineraries via more than two different navigation properties in my test.

    So if you have some other ideas about it, please share them.

    And I do not think it is needed to write two queries.

    We can write the query logic as below:

    select * from Rollings r join Itineraries i on
    
    (r.Id = i.TrailerId or r.Id=i.VehicleId)
    
    where r.Id=1

    The result will return records that whether TrailerId =1 or VehicleId =1

    Regards.



    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 23, 2013 9:27 AM
    Moderator