EF Not Lazy Loading ApplicationUser

Опитвам се да разбера защо EF мързеливо зарежда всичко освен моето свойство 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();
}

Пропускам ли нещо тук? Това работи известно време и внезапно спря, нямам представа защо... кодовата база не се е променила според регистрационните файлове за ангажиране.


person devfunkd    schedule 31.10.2014    source източник


Отговори (1)


Рамката на обекта не знае към какъв ключ да го съпостави, защото нямате свойство с име „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; }
}
person TysonWolker    schedule 31.10.2014
comment
Мислех, че работи от името на свойството, CustomerId и Customer? Ще опитам това. - person devfunkd; 31.10.2014
comment
наистина, Customer и CustomerId са достатъчни, не е необходимо да указвате атрибута на външния ключ. - person badr slaoui; 04.01.2016