2014-10-31 33 views
0

我想弄清楚爲什麼EF懶惰加載一切,但我的ApplicationUser屬性。我正在使用以下域對象的通用存儲庫模式。EF不延遲加載應用程序用戶

public class Order 
{ 
    [Key] 
    public Guid Id { get; set; } 
    public int PaymentTransactionId { get; set; } 
    public string CustomerId { get; set; } 
    public int ChildId { get; set; } 
    public DateTime PickUpDate { get; set; } 
    public PickUpTime PickUpTime { get; set; } 
    public string Notes { get; set; } 
    public decimal Discount { get; set; } 
    public decimal SubTotal { get; set; } 
    public decimal Tax { get; set; } 
    public decimal Total { get; set; } 
    public DateTime DateCreated { get; set; } 
    public string CreatedBy { get; set; } 
    public OrderStatus Status { get; set; } 

    public virtual ApplicationUser Customer { get; set; } 
    public virtual Child Child { get; set; } 
    public virtual PaymentTransaction PaymentTransaction { get; set; } 
    public virtual PromotionCode PromotionCode { get; set; } 
} 

我試着做,當我從數據庫中檢索實體以下

context.Configuration.LazyLoadingEnabled = true; 

所有虛擬除ApplicationUser性能得到填充。

的DbContext

public class DatabaseContext : IdentityDbContext<ApplicationUser> 
{ 
    public DatabaseContext() 
     : base("name=DefaultContext") 
    { 
     Database.SetInitializer<DatabaseContext>(null); 
     Configuration.LazyLoadingEnabled = true; 
    } 

    public IDbSet<PromotionCode> Promotions { get; set; } 
    public IDbSet<PaymentTransaction> PaymentTransactions { get; set; } 
    public IDbSet<BakeryOrder> BakeryOrders { get; set; } 
    public IDbSet<Child> Children { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<BakeryOrder>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 

     base.OnModelCreating(modelBuilder); 
     modelBuilder.Entity<IdentityUser>() 
      .ToTable("Users"); 
     modelBuilder.Entity<ApplicationUser>() 
      .ToTable("Users"); 
    } 

    public static DatabaseContext Create() 
    { 
     return new DatabaseContext(); 
    } 
} 

REPOSITORY

public class Repository<T> : IRepository<T> where T : class 
    { 
     protected DatabaseContext Context; 

     public Repository(DatabaseContext context) 
     { 
      Context = context; 
     } 

     public IEnumerable<T> Get() 
     { 
      return Context.Set<T>(); 
     } 
} 

服務

public IEnumerable<Order> Get() 
{ 
    return _orderRepository.Get(); 
} 

我失去了一些東西在這裏?這確實工作了一段時間,突然停了下來,我不知道爲什麼......代碼庫沒有根據提交日誌進行更改。

回答

0

實體框架不知道將其映射到哪個鍵,因爲您沒有任何名爲「ApplicationUserId」的屬性,因此您必須顯式添加指向右外鍵的屬性。

public class Order 
{ 
    [Key] 
    public Guid Id { get; set; } 
    public int PaymentTransactionId { get; set; } 
    public string CustomerId { get; set; } 
    public int ChildId { get; set; } 
    public DateTime PickUpDate { get; set; } 
    public PickUpTime PickUpTime { get; set; } 
    public string Notes { get; set; } 
    public decimal Discount { get; set; } 
    public decimal SubTotal { get; set; } 
    public decimal Tax { get; set; } 
    public decimal Total { get; set; } 
    public DateTime DateCreated { get; set; } 
    public string CreatedBy { get; set; } 
    public OrderStatus Status { get; set; } 
    [ForeignKey("CustomerId")] 
    public virtual ApplicationUser Customer { get; set; } 
    public virtual Child Child { get; set; } 
    public virtual PaymentTransaction PaymentTransaction { get; set; } 
    public virtual PromotionCode PromotionCode { get; set; } 
} 
+0

我以爲它是從屬性名稱,CustomerId和Customer?我會試試這個。 – devfunkd 2014-10-31 13:13:17

+0

確實,Customer和CustomerId就足夠了,你不需要指定外鍵屬性。 – 2016-01-04 08:01:44