You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support partial loading (i.e. not eagerly loading all scalar properties) #1387
Can you please add partial loading and by that I mean in addition to lazy and eager loading of relationships, the third option - lets call it partial for now - loads the related entity but only containing the primary keys and no other data. If needed I could resolve the rest of the data manually.
For example I have a Contact entity with one to many relationship with Address. With partial loading, I will get Contact.Address.AddressId only. I could then resolve the rest of the properties manually. Maybe all maybe the ones I specify via DbContext.Entry(address).Resolve(a=>a.Line1;....
Reacted by Glen, MarcoLoetscher, Wain123, Paweł Pietrasz, 935main, Sascha Patschka, Andrew Scott, SemicolonStruggles, Dai, Alexander Prokhorov and 43 more
changed the title [-]Support partial loading (i.e. loading of stubs)[/-][+]Support partial loading (i.e. not eagerly loading all scalar properties)[/+]on Aug 17, 2017
As a workaround (and that's good enough for me), I do something like this:
Let's assume we have a "estado" table with the field Id as PK and a big text column called "observaciones". This is how you define the entities:
public partial class Estado
{
public Guid Id { get; set; }
public DateTime Fecha { get; set; }
public Observaciones Observaciones { get; set; }
}
public class Observaciones
{
public Guid Estado { get; set; }
public string Texto { get; set; }
public Estado Estado { get; set; }
}
modelBuilder.Entity<Observaciones>(entity =>
{
entity.HasKey(e => e.Id)
.HasName("PK_estado");
entity.HasOne(e => e.Estado).WithOne(o => o.Observaciones).HasForeignKey<Estado>(e => e.Id);
entity.Property(e => e.Texto)
.HasColumnName("observaciones")
.HasColumnType("text");
entity.ToTable("estado", "reactivos");
});
modelBuilder.Entity<Estado>(entity =>
{
entity.HasKey(e => e.Id)
.HasName("PK_estado");
entity.ToTable("estado", "reactivos");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.Fecha).HasColumnName("fecha");
entity.HasOne(e => e.Observaciones).WithOne(o => o.Estado).HasForeignKey<Observaciones>(e => e.Id);
});
This works with EF 2.0 (you need to Include() to load observaciones column) I guess that this will lazy load if you define Observaciones and Estado as virtual in EF 2.1, but I haven't tested it.
@faibistes Does this produce one query or 2 queries against the same table? If one query, is the table joining with itself on the Id? Any issues from this?
I have tested faibistes solutions and it worked for EF Core 2.1.
also I tired to set Observaciones as Owned Entity but I figured out that Owned Entities are included by default.
If we had an option for owned Entities to not be included by default and be in select part only when they are included, that would be a cleaner solution.
Ok, anyway the downside of using table split is adding an unnecessary complexity to data model that can be a high cost for big data models.
It would be more simple if we can do something like these:
var images = _context.Set<Image>()
.Ignore(i => i.Content)
.ToList();
Are there any approaches that are less complicated than @hmdhasani ?
I need to ignore some audit properties (createdDate, userId,...) when querying from db (reduce extra step to encrypt/decrypt) but I also want to add or update them.
Reacted by Marcos Almeida OOO, Julien MOLINA, codes-notify and RouR
@ssougnez see above for the workaround (i.e. project out the properties you want with Select). Otherwise this issue is in the backlog for now.
Hello,
The workarounds mentioned above are, as you say, workarounds... It's not realistic to have to manually specify all properties you want.
Imagine an entity with 20 fields and you just want to exclude the description one, will you specify the 19 other ones? And each time you add a field, you have to add it to your query (but will surely forget to do so until issues arise...).
Having an Ignore method that simply ignores some of the mentioned fields seems pretty straightforward and useful to me (not saying the implementation is :-)).
I find it amusing that my decade old issue #2849 was closed as a duplicate of this issue. I'd like to point out that this issue talks about partial loading of related entitles, while my issue talks about loading a partial of the entity itself.
Is there a good way to solve this problem now?
I am very surprised that this problem has existed for such a long time. In the past, when I used ThinkPHP, it was very easy to exclude a certain field.
Activity
[-]Partial Loading Support[/-][+]Support partial loading (i.e. loading of stubs)[/+]ajcvickers commentedon Aug 17, 2017
See #9385 for some more ideas in this area.
[-]Support partial loading (i.e. loading of stubs)[/-][+]Support partial loading (i.e. not eagerly loading all scalar properties)[/+]ajcvickers commentedon Aug 17, 2017
See #8138 for additional ideas in this area--specifically around lazy loading of scalar properties.
faibistes commentedon May 25, 2018
As a workaround (and that's good enough for me), I do something like this:
Let's assume we have a "estado" table with the field Id as PK and a big text column called "observaciones". This is how you define the entities:
This works with EF 2.0 (you need to
Include()to load observaciones column) I guess that this will lazy load if you define Observaciones and Estado as virtual in EF 2.1, but I haven't tested it.bbrandt commentedon Mar 14, 2019
@faibistes Does this produce one query or 2 queries against the same table? If one query, is the table joining with itself on the Id? Any issues from this?
inlineHamed commentedon May 16, 2019
I have tested faibistes solutions and it worked for EF Core 2.1.
also I tired to set Observaciones as Owned Entity but I figured out that Owned Entities are included by default.
If we had an option for owned Entities to not be included by default and be in select part only when they are included, that would be a cleaner solution.
AndriySvyryd commentedon May 16, 2019
@hmdhasani The solution @faibistes showed is Table splitting, that's the recommended way to lazy load scalar properties.
Owned entities will always be eagerly loaded.
inlineHamed commentedon May 17, 2019
Ok, anyway the downside of using table split is adding an unnecessary complexity to data model that can be a high cost for big data models.
It would be more simple if we can do something like these:
And
I wonder is there currently any way to do this? ( maybe by somehow interposition or manipulation of query generation process )
inlineHamed commentedon May 24, 2019
After trying some approaches I ended up with this workaround:
The base idea is to turn for example this query:
into this:
by overriding DefaultQuerySqlGenerator.GenerateList()
when the query is like this:
I added AsNoTracking() to become sure the value of ignored column will not change.
here is the full code: ( most parts were taken from https://www.chasingdevops.com/sql-generation-ef-core/ )
it requires this in DbContext:
QuanTran91 commentedon May 26, 2020
Are there any approaches that are less complicated than @hmdhasani ?
I need to ignore some audit properties (createdDate, userId,...) when querying from db (reduce extra step to encrypt/decrypt) but I also want to add or update them.
AndriySvyryd commentedon Jun 13, 2020
See #21251 for ideas on how to combine with
IncludeQuanTran91 commentedon Jun 29, 2020
Thanks @AndriySvyryd but it would be better if it has Exclude :)
28 remaining items
ssougnez commentedon Jun 16, 2025
Hello,
The workarounds mentioned above are, as you say, workarounds... It's not realistic to have to manually specify all properties you want.
Imagine an entity with 20 fields and you just want to exclude the description one, will you specify the 19 other ones? And each time you add a field, you have to add it to your query (but will surely forget to do so until issues arise...).
Having an
Ignoremethod that simply ignores some of the mentioned fields seems pretty straightforward and useful to me (not saying the implementation is :-)).dmlsrc commentedon Jun 20, 2025
I find it amusing that my decade old issue #2849 was closed as a duplicate of this issue. I'd like to point out that this issue talks about partial loading of related entitles, while my issue talks about loading a partial of the entity itself.
AndriySvyryd commentedon Jun 21, 2025
@ltctech This issue tracks partial loading of both related and the main entities.
luomo-pro commentedon Feb 6, 2026
Is there a good way to solve this problem now?
I am very surprised that this problem has existed for such a long time. In the past, when I used ThinkPHP, it was very easy to exclude a certain field.
Fixes #37278
Fixes #37278
Fixes #37278
CodesInChaos commentedon May 6, 2026
@AndriySvyryd your recent commits mention this issue. Are there any concrete plans for supporting this issue in one of the next releases?
AndriySvyryd commentedon May 6, 2026
We'll consider it, but it's unlikely to make it for the next couple of releases