Skip to content

Use Projectable without Select #84

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

Closed
zoriya opened this issue Sep 9, 2023 · 2 comments
Closed

Use Projectable without Select #84

zoriya opened this issue Sep 9, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@zoriya
Copy link
Contributor

zoriya commented Sep 9, 2023

Assuming this example:

public record Entity
{
    public int Id { get; set; }
    
    [Projectable(UseMemberBody = nameof(Computed2))]
    public int Computed1 { get; set; }

    private int Computed2 => Id * 2;
}

I would like this to work:

public void Test()
{
	Entity foo = database.Set<Entity>().First();
	Assert(foo.Computed1 == foo.Id * 2);
}

with a generated query like that:

SELECT e.id, computed1 as e.Id * 2  FROM entities as e LIMIT 1

Right now, this example fails because the generated query is never rewritten. The actual query looks like this:

SELECT e.id, e.computed1 FROM entities as e LIMIT 1

So the SQL provider throws because computed1 is not a valid column.

@yinzara
Copy link

yinzara commented Oct 23, 2023

I would REALLY love this feature as well.

I'd probably called this "Materialized Projectioables".

I would like the ability to optionally include a projectable property so that it's projected into the primary entity when it's loaded so that I don't need the related data to be included on the entity without having to include the related data.

Take this example:

public class MyEntity
{
   public Guid Id { get; set; }

   public MyOtherEntity MyOtherEntity { get; set; } = null!;
   public Guid MyOtherEntityId { get; set; }

   [Projectable(UseMemberBody(nameof(MyOtherEntityName))
   public string MyOtherEntityNameIncluded { get; set; }

   [Projectable] public string MyOtherEntityName => this.MyOtherEntity.Name;
}

public class MyOtherEntity
{
   public Guid Id { get; set; }
   public string Name { get; set; }
   ... a lot of other properties and potentially nested owned relationships so loading this entity is expensive
}
dbContext.DbSet<MyEntity>.Include(_ => _.MyOtherEntityNameIncluded)
// or maybe something custom
dbContext.DbSet<MyEntity>.Materialize(_ => _.MyOtherEntityNameIncluded)

If I don't have the materialized properties, I would have to o:

dbContext.DbSet<MyEntity>.Include(_ => _.MyOtherEntity)

which is extremely inefficient

@zoriya
Copy link
Contributor Author

zoriya commented Oct 23, 2023

When #86 gets merged, you would not need anything for your .MyOtherEntityNameIncluded to work.

dbContext.DbSet<MyEntity>.First()

would already include your MyOtherEntityNameIncluded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants