-
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
How to use ToQuery outside the DbContext? #18957
Labels
Comments
@Neme12 I just tested this and it works for me. public sealed class RecordDataConfiguration : IEntityTypeConfiguration<RecordData>
{
private readonly DbContext _context;
public RecordDataConfiguration(DbContext context)
{
_context = context;
}
public void Configure(EntityTypeBuilder<RecordData> builder)
{
builder.HasNoKey().ToQuery(() => _context.Set<Record>().Select(r => new RecordData { Value = r.Value }));
}
} protected override void OnModelCreating(ModelBuilder modelBuilder)
{
new RecordDataConfiguration(this).Configure(modelBuilder.Entity<RecordData>());
} It's probably also worth noting that |
ajcvickers
added
closed-no-further-action
The issue is closed and no further action is planned.
and removed
type-enhancement
area-query
labels
Nov 19, 2019
If it works for you then it works for everyone. |
@ajcvickers due to model caching is there not a chance that will capture the initial context only? |
@Snappyfoo I don't think so, no. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
In a
DbContext
'sOnModelCreating
method, I can useToQuery
to map an entity type to a LINQ query as opposed to a table:but in our project, we like to use
IEntityTypeConfiguration
classes to configure each entity type and then callApplyConfiguration
from inside theDbContext
as opposed to having any configuration directly there.My question is, how to define the query passed to
ToQuery
in such a configuration class outside of theDbContext
?I would expect the lambda in
ToQuery
to take aDbContext
as the parameter so that I could do something like this:but I don't see that being the case and can't think of any way to get the
DbContext
orDbSet
for a specific entity type here. Am I missing something? What's the correct way to do this?The text was updated successfully, but these errors were encountered: