【问题标题】:EF Not Lazy Loading ApplicationUserEF 不延迟加载 ApplicationUser
【发布时间】:2014-10-31 02:30:24
【问题描述】:

我试图弄清楚为什么 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();
}

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

【问题讨论】:

    标签: c# asp.net-mvc entity-framework dbcontext


    【解决方案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; }
    }
    

    【讨论】:

    • 我认为它可以通过属性名称、CustomerId 和 Customer 起作用?我试试看。
    • 确实,Customer和CustomerId就够了,不用指定外键属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多