-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UseLazyLoadingProxies does not work when model is built outside of OnModelCreating #15532
Comments
@pravinkarthy Can you post the code you are using to build the model? |
@ajvickers Thanks for responding. Here is the code that I use for ModelBuilding. public class ModelBuilderService
{
private static IModel GetOrCreateCompiledModel(IEnumerable<string> modelSupplyingAssemblyPatterns)
{
var conventions = SqlServerConventionSetBuilder.Build();
var modelBuilder = new ModelBuilder(conventions);
var modelBuilderType = typeof(ModelBuilder);
var entityMethod = modelBuilderType.GetMethod("Entity", modelBuilderType.GetGenericArguments());
var pathToUse = AppDomain.CurrentDomain.BaseDirectory;
if (!AppDomain.CurrentDomain.BaseDirectory.Contains("bin"))
{
pathToUse = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin");
}
var entitiesAdded = new HashSet<string>();
if (entityMethod == null)
{
throw new NullReferenceException("Cannot find Entity method on DbModelBuilder");
}
foreach (var assemblyPattern in modelSupplyingAssemblyPatterns)
{
var dataProviderModels = Directory.EnumerateFiles(pathToUse, assemblyPattern, SearchOption.AllDirectories);
foreach (var dll in dataProviderModels)
{
var assembly = Assembly.LoadFrom(dll);
modelBuilder.ApplyConfigurationsFromAssembly(assembly);
var typesToRegister = assembly.GetTypesInheritingFrom<BaseObject>();
foreach (var entity in typesToRegister)
{
if (entitiesAdded.Add(entity.FullName))
{
entityMethod.MakeGenericMethod(entity)
.Invoke(modelBuilder, new object[] { });
}
}
}
}
return modelBuilder.Model;
}
} |
@pravinkarthy Thanks! Note for triage: the issue here is that the proxies package adds a convention, but there is no way for this to be picked up when creating the model builder externally. |
@ajcvickers We used the same code in EF6 for lazyloading. But on our .NET core transformation, got blocked with this issue with EFCore since we follow a generic pattern to fetch entities. |
@ajcvickers Tried injecting the ILazyLoader service into an entity as a workaround but even that did not help for fetching many-many related entities where data was loading only for the first entity of many-many relationship but did not load the data for the second object. ` public partial class PersonOrganisation
}` On LazyLoading of this "PersonOrganisations" collection, each "PersonOrganisation" object had only "Person" object in "PersonOrganisation" Entity getting fetched but the "Organisation" object was null and not fetched. |
@pravinkarthy We haven't fully investigated this yet, but my initial thoughts are that it is the |
On analysis, found out that the lazyloadingproxies applies conventions after the entities are built with "OnModelCreating()". But in this case where models are built outside using modelbuilder, we are forced to set the conventionset object during the Modelbuilder object creation itself and then adding the entities to the Modelbuilder which seems to be root-cause of the issue here. |
Notes from triage: we will allow extensions to expose conventions in a way that they can then be plugged into a stand-alone model builder. |
Is the issue solved or still in backlog? |
@Luigi6821 As the milestone indicates, this is still in the backlog. |
Lazy loading of associated entity objects does not work when model is built outside of OnModelCreating and the associated object despite keeping all the methods virtual.
Eg.,
Further technical details
Lazy loading works only when models are built overriding onModelCreating() method
EF Core version: 2.2.4
Database Provider: 2.2.4
Operating system: Windows
IDE: Visual Studio 2017 15.4
The text was updated successfully, but these errors were encountered: