I have project in Entity Framework Core 3.1. When I use lazy loading like this:
services.AddDbContext<IQContext>(options => options.UseLazyLoadingProxies().UseSqlServer(...)
and I call this:
public async Task<Guid> UpdateAsync(object entity ...)
{
...
Type entityType = entity.GetType();
string primaryKeyName = _dbContext.Model.FindEntityType(entityType).FindPrimaryKey().Properties.Select(x => x.Name).Single();
}
I got this error:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Microsoft.EntityFrameworkCore.ModelExtensions.FindEntityType(...) returned null.
But when I remove the UseLazyLoadingProxies(), everything works.
Any ideas what can be wrong or how to fix it?