2014-10-31 5 views
0

Я пытаюсь понять, почему EF лениво загружает все, кроме моего свойства ApplicationUser. Я использую общий шаблон репозитория со следующим объектом домена.EF Not Lazy Loading ApplicationUser

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

СКЛАД

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

Entity framework не знает, к какому ключу его отобразить, потому что у вас нет свойства с именем «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

+0

действительно, Customer и CustomerId достаточно, вам не нужно указывать атрибут внешнего ключа. –