5

I've separated the Read Context from Write Now I'm going to enable LazyLoading in ReadOnlyContext by default. I also used the following method, but unfortunately it does not work.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder
        .UseLazyLoadingProxies()
        .UseSqlServer(@"Data Source=.;Initial Catalog=UniversityDb;Persist Security Info=True;User ID=admin;Password=asdasdsadasd");
}

my model:

public class Partner : BaseEntity<int>
{
    public string Name { get; set; }
    public DateTime CreateDate { get; set; }
    public bool IsDisabled { get; set; }
    public bool IsDeleted { get; set; }
    public virtual ICollection<PartnerUser> PartnerUsers { get; set; }
}

my ef version:

EntityFramework core v 2.1.2

public async Task<PartnerQuery> Get(int id)
{
    var result = await _partnerDbSet.SingleAsync(c => c.Id == id);
    var list = result.PartnerUsers;
    return new PartnerQuery()
    {
        CreateDate = result.CreateDate,
        Name = result.Name,
        Id = result.Id
    };
}

I got this error:

"Error generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning: An attempt was made to lazy-load navigation property 'PartnerUsers' on detached entity of type 'PartnerProxy'. Lazy-loading is not supported for detached entities or entities that are loaded with 'AsNoTracking()'.'. This exception can be suppressed or logged by passing event ID 'CoreEventId.DetachedLazyLoadingWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'."

How can this problem be solved?

12
  • what is the ef core version ?
    – Eldho
    Commented Sep 24, 2018 at 8:47
  • @Eldho I edited the post
    – John
    Commented Sep 24, 2018 at 8:48
  • 1
    your navigation is marked as virtual for lazyloading. Take a look tat his learn.microsoft.com/en-us/ef/core/querying/…
    – Eldho
    Commented Sep 24, 2018 at 8:50
  • @Eldho I did exactly what you said on the link you sent, but the problem did not resolve
    – John
    Commented Sep 24, 2018 at 9:14
  • are you getting any error. how do you evaluate this
    – Eldho
    Commented Sep 24, 2018 at 9:15

2 Answers 2

8

You are using AsNoTracking() somewhere in your code, Lazy Loading won't work when using AsNoTracking() method.

You have two options:

  1. Use the Include() method to load your relationships

  2. Ignore the warning and simply get null for your relationships

You can configure EF to ignore this error:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder
        .UseLazyLoadingProxies()
        .ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.DetachedLazyLoadingWarning))
        .UseSqlServer(@"Data Source=.;Initial Catalog=UniversityDb;Persist Security Info=True;User ID=admin;Password=asdasdsadasd");
}
2

If you are using UseLazyLoadingProxies(), you have to use CreateProxy() to enable Lazy Loading.

public async Task<PartnerQuery> Get(int id)
{
  var result = await _partnerDbSet.SingleAsync(c => c.Id == id);
  var list = result.PartnerUsers;
  // assuming you have access to your database context
  var model = myDatabaseContext.CreateProxy<PartnerQuery>();

  model.CreateDate = result.CreateDate;
  model.Name = result.Name;
  model.Id = result.Id;

  return model;
}

ProxyExtensions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.