Expose a non-generic DbSet to support operations with shared-type entity types
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start by reading the existing strongly typed FromSqlRaw methods in RelationalQueryableExtensions and the proposed non-generic IQueryable overload. Trace how the DbContext and entity metadata are used for shared-type and navigation entities. Done means establishing whether the API can support this scenario and defining the required behavior and tests.
Written by the indexing model from the issue text.
Description
Assume a DbContext with two related DBSet<> properties like so:
public MyDbContext : DbContext
{
public virtual DbSet<Supplier> Account { get; set; }
public virtual DbSet<Part> Account { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
. . .
EntityTypeBuilder<Part> partEntity
.HasOne(d => d.Supplier)
.WithMany(p => p.Part)
.HasForeignKey(d => d.SupplierId);
. . .
}
No means exists (that I could find) to be able to the following:
public static IEnumerable DoAwesomeStuff<TEntity>(this DbSet<TEntity> dataSource, params object[] otherParameters)
where TEntity : class
{
string sql = "...Stuff I did to generate this internally...";
var dbContext = (dataSource as IInfrastructure<IServiceProvider>).GetService<ICurrentDbContext>().Context;
// Get the result
var dbSet = dbContext.Set<TEntity>();
var parentResults = dbSet.FromSqlRaw(sql);
// Do other logic to hydrate navigation properties
string childSql = "...more stuff I did to create this for a navigation property...";
string navigationPropertyName = (string)otherParameters[0];
IEntityType entityType = dbContext.Model.FindEntityType(typeof(TEntity));
var navigationProperties = entityType.GetDeclaredNavigations();
var navigationProperty = navigationProperties.Single(p => p.Name == navigationPropertyName);
var navigationEntityType = navigationProperty.GetTargetType();
// I wrote an extension method to Get the DbSet as an IQueryable.
// It was the best I could do...
var childDbSet = dbContext.GetDbSetForClrType(navigationEntityType.ClrType);
// But even with this, all the extension methods defined in
// RelationalQueryableExtensions extend a strongly typed to a DbSet<>
var childResults = childDbSet.FromSqlRaw(sql); // <-- Not currently possible
}
private static IQueryable GetDbSetForClrType(this DbContext dbContext, Type clrType)
{
Type DbSetType = typeof(DbSet<>);
var propertyInfo = dbContext.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Select(p => new { PropertyInfo = p, PropertyType = p.PropertyType })
.First(t =>
t.PropertyType.IsGenericType &&
t.PropertyType.GetGenericTypeDefinition() == DbSetType &&
t.PropertyType.GetGenericArguments()[0] == clrType)
.PropertyInfo;
return propertyInfo.GetValue(dbContext) as IQueryable;
}
I want this because I wrote my own Uri query string parser from which I use certain information to decide which parts of a DbContext to hydrate. (Consider for example, the $expand operator that OData uses. Not only is the main entity queried, but the related entities are queried as well.)
If I want more control of the SQL generated, some solution to the above would work best.
Would it be possible to add similar methods in RelationalQueryableExtensions without the strong typing?
For example:
[StringFormatMethod("sql")]
public static IQueryable FromSqlRaw(
[NotNull] this IQueryable source,
[NotNull] [NotParameterized] string sql,
[NotNull] params object[] parameters)
{
Check.NotNull(source, nameof(source));
Check.NotEmpty(sql, nameof(sql));
Check.NotNull(parameters, nameof(parameters));
var queryableSource = (IQueryable)source;
return queryableSource.Provider.CreateQuery(
GenerateFromSqlQueryRoot(
queryableSource,
sql,
parameters));
}
- 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