我遇到了一些奇怪的问题,我有一个从现有数据库生成的 EDMX,并且外键是在生成的类中捕获的,但是在与
Include()
使用它们时,我收到错误。以下是 Context 和 ENtities 的提取代码:
上下文:
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
}
实体:
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; }
}
这是发生错误的 DAL:
DAL 代码:
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;
}
}
错误在这些行中:
objAddReservation = context.MajlisReservations
.Include(i => i.MajlisPassengerDetails)
.Include(i => i.AlMajlisServices)
错误:
Cannot convert lambda expression to type 'string' because it is not a delegate type
并且
i
有红色波浪状读数:无法推断委托类型
我也有一个绿色的波浪形
context.MajlisReservations
:
可能对数据库进行多次查询以查找相关实体(N+1 问题)。以下导航可能需要包含在 查询:'Reservation.PassengerDetails'、'Reservation.AppliedServices'
行动称为
Load required relations
如果我选择这个选项,我会得到这个:
var objAddReservation = QueryableExtensions.Include(QueryableExtensions.Include(context.MajlisReservations, majlisReservation => majlisReservation.MajlisPassengerDetails), majlisReservation => majlisReservation.AlMajlisServices)
.SingleOrDefault(i => i.MajlisReservationId == objReservation.MajlisReservationId);
针对此方法编译代码,但是
在 EF6 中,
Include(=>)
的扩展方法由 QueryableExtensions
命名空间中的 System.Data.Entity
提供,因此要识别它,您需要确保使用 Include()
风格的任何类都声明了 using System.Data.Entity;
。
Include()
上的默认 IQueryable<T>
方法只是 string
变体,它抱怨无法将 Lambda 转换为它。