-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof of concept: ad-hoc entity types
This is primarily for #10753 This is a draft; comments, exceptions, some testing intentionally left not done yet. The idea is to build and ad-hoc entity type which can then used for a raw SQL query. However, it actually also allows composing over the raw SQL query and ad-hoc LINQ queries. Things to consider: - The entity type cannot have relationships - Properties are mapped by convention, but mapping attributes are respected. This is more important for LINQ queries than raw SQL queries since it allows EF to target the correct table and columns. - The entity types are keyless. We could support keys, which would open up inserts, updates, and deletes, but I'm not sure we should. - This happens automatically if the CLR type is not mapped. This means no new API service, but it also means it could be easy to accidentally use an ad-hoc type if you forget to add the type to the model. On the other hand, and type with navigations will fail anyway, so perhaps with a reasonable exception message its okay.
- Loading branch information
1 parent
30f26cf
commit 74bd92d
Showing
20 changed files
with
3,046 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.ComponentModel.DataAnnotations.Schema; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Metadata; | ||
|
||
#pragma warning disable CS1591 | ||
|
||
public class RelationalAdHocMapper : AdHocMapper | ||
{ | ||
public RelationalAdHocMapper(AdHocMapperDependencies dependencies) | ||
: base(dependencies) | ||
{ | ||
} | ||
|
||
public override IEntityType MapAdHocEntityType(Type clrType) | ||
{ | ||
var entityType = CreateEntityType(clrType); | ||
|
||
var relationalModel = (RelationalModel)Dependencies.Model.GetRelationalModel(); | ||
|
||
var tableMappings = new List<TableMapping>(); | ||
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.DefaultMappings, tableMappings); | ||
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.TableMappings, tableMappings); | ||
|
||
var (tableName, schema) = GetTableName(clrType); | ||
var table = new Table(tableName, schema, relationalModel); | ||
var tableMapping = new TableMapping(entityType, table, false); | ||
tableMappings.Add(tableMapping); | ||
|
||
foreach (var (member, field, propertyName) in GetMembersToMap(clrType)) | ||
{ | ||
var property = CreateProperty(propertyName, member, field, entityType); | ||
|
||
var columnName = GetColumnName(propertyName, member, field); | ||
var column = new Column(columnName, ((RelationalTypeMapping)property.TypeMapping).StoreType, table) | ||
{ | ||
IsNullable = ((IReadOnlyProperty)property).IsNullable | ||
}; | ||
|
||
table.Columns.Add(column.Name, column); | ||
var columnMapping = new ColumnMapping(property, column, tableMapping); | ||
tableMapping.AddColumnMapping(columnMapping); | ||
column.AddPropertyMapping(columnMapping); | ||
|
||
var columnMappings = new SortedSet<ColumnMapping>(ColumnMappingBaseComparer.Instance); | ||
property.AddRuntimeAnnotation(RelationalAnnotationNames.TableColumnMappings, columnMappings); | ||
columnMappings.Add(columnMapping); | ||
} | ||
|
||
return ((RuntimeModel)Dependencies.Model).GetOrAddAdHocEntityType(entityType); | ||
} | ||
|
||
protected virtual (string TableName, string? Schema) GetTableName(Type clrType) | ||
{ | ||
var tableAttribute = clrType.GetCustomAttributes<TableAttribute>(inherit: false).FirstOrDefault(); | ||
|
||
return (tableAttribute?.Name ?? clrType.Name, tableAttribute?.Schema); | ||
} | ||
|
||
protected virtual string GetColumnName(string propertyName, MemberInfo member, FieldInfo? field) | ||
=> member.GetCustomAttributes<ColumnAttribute>(inherit: false).FirstOrDefault()?.Name ?? propertyName; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.Infrastructure; | ||
|
||
/// <summary> | ||
/// <para> | ||
/// Service dependencies parameter class for <see cref="IAdHocMapper" /> | ||
/// </para> | ||
/// <para> | ||
/// This type is typically used by database providers (and other extensions). It is generally | ||
/// not used in application code. | ||
/// </para> | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// Do not construct instances of this class directly from either provider or application code as the | ||
/// constructor signature may change as new dependencies are added. Instead, use this type in | ||
/// your constructor so that an instance will be created and injected automatically by the | ||
/// dependency injection container. To create an instance with some dependent services replaced, | ||
/// first resolve the object from the dependency injection container, then replace selected | ||
/// services using the 'With...' methods. Do not call the constructor at any point in this process. | ||
/// </para> | ||
/// <para> | ||
/// The service lifetime is <see cref="ServiceLifetime.Scoped" />. This means that each | ||
/// <see cref="DbContext" /> instance will use its own instance of this service. | ||
/// The implementation may depend on other services registered with any lifetime. | ||
/// The implementation does not need to be thread-safe. | ||
/// </para> | ||
/// </remarks> | ||
public sealed record AdHocMapperDependencies | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
/// <remarks> | ||
/// Do not call this constructor directly from either provider or application code as it may change | ||
/// as new dependencies are added. Instead, use this type in your constructor so that an instance | ||
/// will be created and injected automatically by the dependency injection container. To create | ||
/// an instance with some dependent services replaced, first resolve the object from the dependency | ||
/// injection container, then replace selected services using the 'With...' methods. Do not call | ||
/// the constructor at any point in this process. | ||
/// </remarks> | ||
[EntityFrameworkInternal] | ||
public AdHocMapperDependencies( | ||
IModel model, | ||
ITypeMappingSource typeMappingSource, | ||
IDiagnosticsLogger<DbLoggerCategory.Model> logger) | ||
{ | ||
Model = model; | ||
TypeMappingSource = typeMappingSource; | ||
Logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// The <see cref="IModel"/>. | ||
/// </summary> | ||
public IModel Model { get; init; } | ||
|
||
/// <summary> | ||
/// The <see cref="TypeMappingSource"/>. | ||
/// </summary> | ||
public ITypeMappingSource TypeMappingSource { get; init; } | ||
|
||
/// <summary> | ||
/// The logger. | ||
/// </summary> | ||
public IDiagnosticsLogger<DbLoggerCategory.Model> Logger { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.