繁体   English   中英

EF不延迟加载ApplicationUser

[英]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之外的所有虚拟属性。

数据库上下文

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

我在这里想念什么吗? 这确实工作了一段时间,突然停止了,我不知道为什么...代码库没有根据提交日志进行更改。

实体框架不知道将其映射到哪个键,因为您没有任何名为“ 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; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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