我试图找出为什么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();
}
}
REPOSITORY
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>();
}
}
SERVICE
public IEnumerable<Order> Get()
{
return _orderRepository.Get();
}
我在这里遗漏了什么吗?这确实工作了一段时间并突然停止,我不知道为什么......代码库没有根据提交日志进行更改。
答案 0 :(得分:0)
实体框架不知道将它映射到哪个键,因为您没有任何名为“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; }
}