Search code examples
c#entity-framework-6.net-4.8

EF6 Include not detecting foreign keys


I'm having some strange issue where I have an EDMX generated from an exisitng DB and the foreign keys are propelry captured within generated classes but upon using them with Include(), I'm getting errors. Here is extracted code for Context and ENtities:

Context:

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
using System.Linq;

namespace MySystem.DAL.Contexts.Reservation;

public partial class ReservationsEntities : DbContext
{
    public ReservationsEntities() : base("name=ReservationsEntities") { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); }

    public virtual DbSet<AppliedService>  AppliedServices   { get; set; }
    public virtual DbSet<PassengerDetail> PassengerDetails  { get; set; }
    public virtual DbSet<Reservation>     Reservations      { get; set; }

    // trimmed
}

Entities:

using System;
using System.Collections.Generic;
    
namespace MySystem.DAL.Contexts.Reservation;  

public partial class Reservation
{
    public Reservation()
    {
        this.AppliedServices  = new HashSet<AppliedService>();
        this.PassengerDetails = new HashSet<PassengerDetail>();            
    }

    public int                ReservationId { get; set; }
    public Nullable<DateTime> DateReserved  { get; set; }
    public Nullable<int>      CompanyId     { get; set; }
    // fields-a-lot

    public virtual ICollection<AppliedService>  AppliedServices  { get; set; }
    public virtual ICollection<PassengerDetail> PassengerDetails { get; set; }        
}

public partial class AppliedService
{
    public int               ServiceID     { get; set; }    
    public string            ServiceCode   { get; set; }
    public string            ServiceName   { get; set; }
    public Nullable<decimal> ServiceAmount { get; set; }
    
    public virtual Reservation Reservation { get; set; }
}

public partial class PassengerDetail
{
    public int PaxDetailID { get; set; }
    public Nullable<int> PaxMfstID { get; set; }
    public string PaxName { get; set; }
    

    public virtual Reservation Reservation { get; set; }
}

And here is the DAL where errors occur:

DAL Code:

using ReservationNs = MySystem.DAL.Contexts.Reservation;

namespace MySystem.DAL.AccessLayers;

public class ReservationsDal : IDisposable
{    
    public ReservationResponseBDO UpdatesBooking2(ReservationBDO objReservation)
    {
        var context           = new ReservationNs.ReservationsEntities();
        var objRetReservation = new ReservationResponseBDO();

        ReservationNs.Reservation objAddReservation = null;

        objAddReservation = context.Reservations
                                   .Include(i => i.PassengerDetails)
                                   .Include(i => i.AppliedServices)
                                   .SingleOrDefault(i => i.ReservationId == objReservation.ReservationId);
      
        try
        {
            // code-a-lot
        }
        catch (Exception)
        {
            // throw-a-lot
        }

        return objRetReservation;
    }
}

Error is in these lines:

objAddReservation = context.MajlisReservations
                           .Include(i => i.MajlisPassengerDetails)
                           .Include(i => i.AlMajlisServices)

Error:

Cannot convert lambda expression to type 'string' because it is not a delegate type

And the i had a red sqiggly reading: delegate type could not be inferred

I also have a green sqiggly on context.MajlisReservations:

Possible multiple queries to the database for related entities (N+1 problem). The following navigations may need to be included in the query: 'Reservation.PassengerDetails', 'Reservation.AppliedServices'

Action is called Load required relations

If I take that option, I get this:

var objAddReservation = QueryableExtensions.Include(QueryableExtensions.Include(context.MajlisReservations, majlisReservation => majlisReservation.MajlisPassengerDetails), majlisReservation => majlisReservation.AlMajlisServices)
                                           .SingleOrDefault(i => i.MajlisReservationId == objReservation.MajlisReservationId);

Code compiles for this method but

  • I have more complex queries and they fail
  • This is not normal and I must be missing something
  • This is legacy code. I have to update it
  • Looking at SCC, I see no changes in history that I acidently checked in. This was working before.

Solution

  • In EF6 the extension method for Include(=>) is provided by the static QueryableExtensions class in the System.Data.Entity namespace. For it to be recognized you need to ensure any class using that flavour of Include() has a using System.Data.Entity; declared. Once that has been declared with the other using statements then the extension method will be resolved. Unfortunately Intellisense isn't much help in using suggestions for suspected extension methods.

    The default Include() method on IQueryable<T> is just the string variant which it is complaining it cannot convert the Lambda to.