Option to turn off automatic navigation property fix-up
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start with the linked EF Core loading-related-data documentation and the issue discussion to understand the requested navigation-property behavior. The issue names no source files or tests; done would require a defined option that prevents automatic fix-up while retaining explicitly included data, along with appropriate validation.
Written by the indexing model from the issue text.
Description
From loading related data EF Core page is the following tip:
Entity Framework Core will automatically fix-up navigation properties to any other entities that were previously loaded into the context instance. So even if you don't explicitly include the data for a navigation property, the property may still be populated if some or all of the related entities were previously loaded.
I would like an option to turn this property off so that if I choose not to load a navigation property it is not loaded for me. The cost of transporting data that is not needed is slowing response time. If there are 70 gages and each gage has a status then that status has 70 gages. There is a reference looping option mentioned it the loading related data article which I did try but the status object still contained the list of gages which I verified through postman.
Below is an example showing the loading of data and clearing the navigation properties that are automatically "fixed up" by EF Core. I am going to need this code in all of my gets in order to drastically reduce the JSON payload size,.
When we use an Include we know the data that is needed for an object. "Fixing up" is something that is not needed. Please remove this feature or please give us a way to disable it.
Code to load data:
try
{
using ( var context = new MyContext() )
{
res =
( from c in
context.Gages.
Include ( "Status" )
select c ).ToList ();
// code to clear nav property loaded by EF Core
res.ForEach (
g =>
{
if ( g != null && g.Status != null && g.Status.Gages != null )
g.Status.Gages.Clear ();
} );
}
}
catch ( Exception ex )
{
throw ex;
}
Entities
[Table("Gages", Schema = "MYSCHEMA" )]
class Gage
{
#region Properties
[Key]
[System.Runtime.Serialization.DataMember]
public virtual int Gage_RID
{ get; set; }
[Required]
[StringLength(255)]
[System.Runtime.Serialization.DataMember]
public virtual string Gage_ID
{ get; set; }
[System.Runtime.Serialization.DataMember]
public virtual int Status_RID
{ get; set; }
#endregion
#region Navigation Properties
[System.Runtime.Serialization.DataMember]
public virtual Status Status
{ get; set; }
#endregion
}
[Table("Status", Schema = "MYSCHEMA" )]
public class Status
{
#region Properties
[Key]
[DatabaseGenerated ( DatabaseGeneratedOption.Identity )]
[System.Runtime.Serialization.DataMember]
public virtual int Status_RID
{ get; set; }
[Required]
[StringLength(255)]
[System.Runtime.Serialization.DataMember]
public virtual string StatusName
{ get; set; }
#endregion
#region Navigation Properties
[System.Runtime.Serialization.DataMember]
public virtual ICollection<Gage> Gages
{ get; set; }
#endregion
}
public partial class MyContext : DbContext
{
public virtual DbSet<Gage> Gages
{ get; set; }
public virtual DbSet<Status> Status
{ get; set; }
}
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 134
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100