2017-03-12 6 views
1

私はLazyLoadingEnabled = trueとProxyCreationEnabled = trueを持っていても、Lazy Loadingが動作していないようです。私はthisガイドですべてを追ってきました。私はデバッガで自分のガイドを走らせ、私が取得しているエンティティに対してProxyが作成されていないことを確認しました。明らかに、プロキシがなければ、遅延ロードは機能しません。問題は、なぜ私がすべてのルールを守ったとしても(私が信じていても)プロキシが作成されないということです。こちらのモデルです:遅延読み込みがEntity Frameworkで機能していません。 (プロキシが作成されていません)

私はエンティティを受け取るためにしようとしている方法
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); 

     } 
    } 
} 

:あなたは正しくPowerShellのスルーEFの参照を削除してインストールし、再試行してくださいproperly.Can

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

プロキシであったはずのエンティティを取得するためのサンプルコードを追加してください。 –

+0

@GertArnold更新されました。 – reheej

答えて

0

Context.ttファイルが作成されていませんパワーシェルと同じパッケージ。

+0

これはデータベースではありません。そしてpowershellはどちらも適用しません。それはNuGetだ。 –

関連する問題