简体   繁体   中英

Lazy Loading not working in Entity Framework. (Proxy not being created)

I can't seem to get Lazy Loading working, even though I have LazyLoadingEnabled = true and ProxyCreationEnabled = true. I have followed everything in this guide. I ran my guide in the debugger and saw that no Proxy is being created for the entity I'm retrieving so. Obviously, without a proxy, lazy loading will not work. The question is, why is no proxy being created even though (I believe) I have followed all the rules. Here's the model:

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);

        }
    }
}

How I'm trying to receive the entity:

        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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM