简体   繁体   English

EF6 - 延迟加载相关实体始终 null

[英]EF6 - Lazy loading related entity always null

For some reason, the lazy loading of a related entity always turns out NULL. I think I followed all the needed rules to apply for the proxy generation, however I don't seem to be able to resolve this issue.由于某种原因,相关实体的延迟加载总是变成NULL。我认为我遵循了所有需要的规则来申请代理生成,但是我似乎无法解决这个问题。

I have an object tblOrder_Procedure that has a tblRoom and a tblProcedure.我有一个 object tblOrder_Procedure,它有一个 tblRoom 和一个 tblProcedure。 For some reason tblProcedure always loads automatically, but tblRoom doesn't.由于某种原因,tblProcedure 总是自动加载,但 tblRoom 不会。 The FK however is there, but the related entity is just not loaded.然而 FK 在那里,但相关的实体只是没有加载。

All classes have public parameterless constructors.所有类都有公共无参数构造函数。

These are the field definitions:这些是字段定义:

public int? Procedure_UID { get; set; }
public int? Room_UID { get; set; }
public virtual tblRoom tblRoom { get; set; }
public virtual tblProcedure tblProcedure { get; set; }

I don't use annotations, but use the OnModelCreating method of the context with fluent API. The FK's are done like this:我不使用注释,而是使用流利的上下文的 OnModelCreating 方法 API。FK 是这样完成的:

modelBuilder.Entity<tblRoom>()
    .HasMany(e => e.tblOrder_Procedure)
    .WithOptional(e => e.tblRoom)
    .HasForeignKey(e => e.Room_UID);
modelBuilder.Entity<tblProcedure>()
    .HasMany(e => e.tblOrder_Procedure)
    .WithOptional(e => e.tblProcedure)
    .HasForeignKey(e => e.Procedure_UID);

ProxyCreationEnabled and LazyLoadingEnabled are both true by default for the context. ProxyCreationEnabled 和 LazyLoadingEnabled 默认情况下都为真。

What am I missing?我错过了什么?

I'll write down the two most likely possibilities.我会写下两种最有可能的可能性。

-Maybe you are using the.AsNoTracking() extension somewhere explicitly or hidden by another helper that you've written. -也许您正在某处显式使用 .AsNoTracking() 扩展名或被您编写的另一个助手隐藏。

-You can try to add optionsBuilder.UseLazyLoadingProxies(); - 你可以尝试添加optionsBuilder.UseLazyLoadingProxies(); to Context.OnConfiguring(DbContextOptionsBuilder optionsBuilder).到 Context.OnConfiguring(DbContextOptionsBuilder optionsBuilder)。

I figured out what the problem was.我想出了问题是什么。 It had 2 origins:它有两个起源:

  1. I was using an object that was newly created and inserted in the database to try to use lazy loading on.我正在使用新创建并插入数据库的 object 来尝试使用延迟加载。 It turns out that you then need to use.Create() instead of 'new' for creating that object.事实证明,您随后需要使用 .Create() 而不是“new”来创建 object。

  2. Turns out I was using a too-long-lived databasecontext.原来我使用的是一个寿命太长的数据库上下文。 Somewhere before having the problem, the tblProcedure object was already loaded into the context cache and thus the foreign key relation could be resolved.在出现问题之前的某个地方,tblProcedure object 已经加载到上下文缓存中,因此可以解决外键关系。 This made it look like it was lazy loaded, but it actually wasn't.这让它看起来像是延迟加载,但实际上不是。

afaik, if you are using virtual keyword, you should use "include" method. afaik,如果您使用虚拟关键字,则应使用“include”方法。

modelBuilder.Entity<tblRoom>()
    .Include(f=>f.tbl_room)
    .HasMany(e => e.tblOrder_Procedure)
    .WithOptional(e => e.tblRoom),
    .HasForeignKey(e => e.Room_UID);

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

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