【发布时间】: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