问题
I am not interested in disabling lazy loading for the entire context. I want only to selectively disable lazy loading for a few key navigational properties.
With the Code First method, I understand how to selectively disable lazy loading:
public virtual Person Requestor { get; set; } //lazy loading enabled
...
public Person Requestor { get; set; } //lazy loading disabled
However with the Database First method, this is code generated from a template so this modification is going to be lost on the next regeneration.
Is there a way to modify the model or template generator for such selective disabling of lazy loading?
回答1:
I don't know of a way. But if you are going to use the Entity code generator you could build in a warning system so that when the code is re-generated you get notified immediately (depending on your build strategy).
So what I would do is for the selected entites, say the entity is Request
and the property in question is Requestor
then write a test to assert that the property is NOT virtual
[TestMethod()]
public void RequestPropertyRequestor_MustNotBeVirtual() {
PropertyInfo[] properties = typeof(Request).GetProperties()
.Where(p => p.GetGetMethod().IsVirtual).ToArray();
Assert.AreEqual(0, properties.Count(p => p.Name == "Requestor"), "Model Code Regenerated - change the Request Entity");
}
Not sure of the accuracy of the reflection code but you get what i mean. This way when the entities are regenerated and you have amended the code, the test fails. early warning system
OR
you could turn off code generation and use POCO's.
Recommended Change
If you don't wanna turn off code gen then modifying the T4 template is the way to go. Just
- set the "Code Generation Stategy" to
None
in the properties of the EDMX designer so that the default generation doesn't occur. This results in no derivedDbContext
or entity classes - in the EDMX designer, right click on the drawing surface and select "Add Code Generation Item". There should be generators listed there, if not just install one through NuGet. Select the EF5 DbContext one.
- Find the T4 template for the entity generation and modify.
回答2:
You can modify the Entities.tt file. More specifically modify the "AccessibilityAndVirtual" method to handle your specific situation. As this template is specific to your current project you do not have to worry about reuse in other projects. You should be able to selectively define which property names you want to exclude from lazy loading. Happy coding.
回答3:
if I understand you correctly what you want is .Include("Requestor")
Person person =
context.Persons.Include("Requestor").FirstOrDefault();
this would get a Person
and the Requestor
in one trip to the db for that query.
Edit: Looking a bit more, this was assuming you had the Property of Requestor on the Person entity, you can however just change this to the appropriate entity and property.
来源:https://stackoverflow.com/questions/17001275/selective-disabling-of-lazy-loading-with-database-first-method