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.

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.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật