【问题标题】:Lazy Loading not working in Entity Framework. (Proxy not being created)延迟加载在实体框架中不起作用。 (未创建代理)
【发布时间】:2017-03-12 16:58:42
【问题描述】:

我似乎无法让延迟加载工作,即使我有 LazyLoadingEnabled = true 和 ProxyCreationEnabled = true。我遵循了this 指南中的所有内容。我在调试器中运行了我的指南,发现没有为我正在检索的实体创建代理。显然,没有代理,延迟加载是行不通的。问题是,即使(我相信)我遵守了所有规则,为什么没有创建代理。这是模型:

using Core.Auditing;
using Core.Common;
using Core.Domain.Models.CustomFields;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace Core.Domain.Models {

    [Table("Order")]
    public class Order : BaseEntity, IAuditableEntity {

        #region Constructors

        public Order(): base() {
        }

        #endregion

        #region Properties

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int OrderID { get; set; }

        public int? EventID { get; set; }

        public DateTime? PostedDate { get; set; }

        public DateTime? ShippedDate { get; set; }

        [MaxLength(50)]
        [Index(IsUnique = false)]
        public string DisplayID { get; set; }
        public decimal OrderTotal { get; set; }
        public decimal Subtotal { get; set; }
        [NotMapped]
        public decimal ShippingPrice {
            get {
                return ShippingOverridePrice ?? ShippingMethodPrice;
            }
            set
            {
                ShippingPrice = value;
            }
        }
        public decimal ShippingMethodPrice { get; set; }
        public decimal? ShippingOverridePrice { get; set; }
        [NotMapped]
        public decimal HandlingPrice {
            get {
                return HandlingOverridePrice ?? HandlingMethodPrice;
            }
            set
            {
                HandlingPrice = value;
            }
        }
        public decimal HandlingMethodPrice { get; set; }
        public decimal? HandlingOverridePrice { get; set; }
        public decimal TaxTotal { get; set; }
        public decimal TaxableTotal { get; set; }
        public decimal DiscountTotal { get; set; }

        [NotMapped]
        public decimal TotalPayments {
            get {
                return Payments?.Sum(p => p.Amount - p.RefundedAmount) ?? 0;
            }
            set
            {
                TotalPayments = value;
            }
        }

        [NotMapped]
        public decimal BalanceDue {
            get {
                return (Subtotal + ShippingPrice + HandlingPrice + TaxTotal) - (DiscountTotal + TotalPayments);
            }
            set
            {
                BalanceDue = value;
            }
        }

        [ForeignKey("OrderAddress")]
        public int? OrderAddressID { get; set; }

        [ForeignKey("ShoppingCart")]
        public int ShoppingCartID { get; set; }

        [ForeignKey("HandlingMethod")]
        public int? HandlingMethodID { get; set; }

        [ForeignKey("ShippingMethod")]
        public int? ShippingMethodID { get; set; }

        [Required]
        [DefaultValue(1)]
        public OrderStatusEnum OrderStatusID { get; set; }

        [Required]
        [DefaultValue(1)]
        public OrderPaymentStatusEnum OrderPaymentStatusID { get; set; }

        [Required]
        [DateTimeKind(DateTimeKind.Utc)]
        public DateTime OrderDate { get; set; }

        public bool Locked { get; set; }

        public bool Commissionable { get; set; }

        public int? OriginalOrderID { get; set; }

        [ForeignKey("OrderOwner")]
        public int? PersonID { get; set; }

        [ForeignKey("CommissionOwner")]
        public int? CommissionPersonID { get; set; }

        [ForeignKey("PersonPaymentMethod")]
        public int? LegalEntityPaymentMethodID { get; set; }

        [Required]
        public int CurrencyTypeID { get; set; }

        public decimal ExchangeRate { get; set; } = (decimal)1.0;

        public int? BusinessUnitID { get; set; }

        [NotMapped]
        public bool IsHostOrder {
            get {
                if (Event != null && Event.PersonID == PersonID) {
                    return true;
                }
                else {
                    return false;
                }
            }
            set
            {
                IsHostOrder = value;
            }
        }
        #endregion

        #region Relationships

        public virtual BusinessUnit BusinessUnit { get; set; }

        [ForeignKey("OrderAddressID")]
        public virtual Address OrderAddress { get; set; }

        [ForeignKey("ShoppingCartID")]
        public virtual ShoppingCart ShoppingCart { get; set; }

        [ForeignKey("HandlingMethodID")]
        public virtual HandlingMethod HandlingMethod { get; set; }

        [ForeignKey("EventID")]
        public virtual Event Event { get; set; }

        [ForeignKey("ShippingMethodID")]
        public virtual ShippingMethod ShippingMethod { get; set; }

        public virtual List<OrderVolumeTotal> OrderVolumeTotals { get; set; }
        public virtual List<Shipment> Shipments { get; set; }

        public virtual List<Return> Returns { get; set; }

        [ForeignKey("OrderStatusID")]
        public virtual OrderStatus OrderStatus { get; set; }

        [ForeignKey("OrderPaymentStatusID")]
        public virtual OrderPaymentStatus OrderPaymentStatus { get; set; }

        [ForeignKey("PersonID")]
        public virtual Person OrderOwner { get; set; }

        [ForeignKey("CommissionPersonID")]
        public virtual Person CommissionOwner { get; set; }

        [ForeignKey("LegalEntityPaymentMethodID")]
        public virtual LegalEntityPaymentMethod PersonPaymentMethod { get; set; }

        public virtual List<OrderLine> OrderLines { get; set; }

        public virtual List<OrderNote> OrderNotes { get;set;}


        public virtual List<Invoice> Invoices { get; set;}

        [ForeignKey("CurrencyTypeID")]
        public virtual CurrencyType CurrencyType { get; set; }

        public virtual List<Payment> Payments { get; set;}

        public decimal ConsultantPrice { get; set; }

        public virtual List<CustomFieldValueOrder> CustomFieldValues { get; set; }

        public virtual List<SubscriptionRunOrderAssociation> SubscriptionRunAssociations { get; set; }

        #endregion

    }
}

namespace Core.Domain.Models.Mapping {
    using System.Data.Entity.ModelConfiguration;

    public class OrderMap : EntityTypeConfiguration<Order> {
        public OrderMap() {
            //            HasMany(ol => ol.OrderLines)
            //           .WithRequired();

            HasMany(a => a.Shipments)
                .WithMany(b => b.Orders)
                .Map(t => t.MapLeftKey("OrderID")
                .MapRightKey("ShipmentID")
                .ToTable("OrderShipment"));

            HasMany(a => a.Payments)
                .WithOptional(b => b.TargetOrder)
                .HasForeignKey(t => t.TargetOrderID);

            HasMany(a => a.Returns)
                .WithRequired(b => b.Order)
                .HasForeignKey(t => t.OrderID);

        }
    }
}

我如何尝试接收实体:

        var result = new BaseResult<OrderCenterDTO>();
        result.ResultCode = MethodResultCode.Success;

        db.Configuration.LazyLoadingEnabled = true;
        db.Configuration.ProxyCreationEnabled = true;

        var TranslationLanguageID = UserHelper.GetTranslationLanguageID();

        var orderResult = db.Orders
                            .FirstOrDefault(o => o.DisplayID == displayID);

【问题讨论】:

  • 能否请您添加示例代码来检索应该是代理的实体?
  • @GertArnold 已更新。

标签: c# asp.net .net entity-framework proxy


【解决方案1】:

Context.tt 文件未正确创建。您能否通过 powershell 正确删除 ef 引用,然后再次尝试使用 power shell 安装相同的软件包。

【讨论】:

  • 这不是数据库优先的。而且powershell也不适用。它是 NuGet。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-26
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多