Skip to content

Compile generated EntityType classes are susceptible to variable name clashes #35011

@NicholasMoss

Description

@NicholasMoss

When using EFOptimizeContext for build-time optimisation and your have an Entity containing certain property names the optimiser will generate invalid code.

For example source and entity appear to clash in the CreateAnnotations method of the generated {EntityName}EntityType class. Which looks something like this:

public static void CreateAnnotations(RuntimeEntityType runtimeEntityType)
{
    var entity = runtimeEntityType.FindProperty("Entity");
    var source = runtimeEntityType.FindProperty("Source");
    runtimeEntityType.SetOriginalValuesFactory(
        ISnapshot (InternalEntityEntry source) =>
        {
            var entity = ((CompanyAddressEntity)(source.Entity));
return ((ISnapshot)(new Snapshot<string, string>(
                (source.GetCurrentValue<string>(entity) == null ? null : ((ValueComparer<string>)(((IProperty)entity).GetValueComparer())).Snapshot(source.GetCurrentValue<string>(entity))), 
                (source.GetCurrentValue<string>(source) == null ? null : ((ValueComparer<string>)(((IProperty)source).GetValueComparer())).Snapshot(source.GetCurrentValue<string>(source))))));
        });
    runtimeEntityType.SetStoreGeneratedValuesFactory(
        ISnapshot () => Snapshot.Empty);
    runtimeEntityType.SetTemporaryValuesFactory(
        ISnapshot (InternalEntityEntry source) => Snapshot.Empty);
    runtimeEntityType.SetShadowValuesFactory(
        ISnapshot (IDictionary<string, object> source) => Snapshot.Empty);
    runtimeEntityType.SetEmptyShadowValuesFactory(
        ISnapshot () => Snapshot.Empty);
    runtimeEntityType.SetRelationshipSnapshotFactory(
        ISnapshot (InternalEntityEntry source) => Snapshot.Empty);
    runtimeEntityType.Counts = new PropertyCounts(
        propertyCount: 2,
        navigationCount: 0,
        complexPropertyCount: 0,
        originalValueCount: 2,
        shadowCount: 0,
        relationshipCount: 0,
        storeGeneratedCount: 0);
    runtimeEntityType.AddAnnotation("Relational:FunctionName", null);
    runtimeEntityType.AddAnnotation("Relational:Schema", null);
    runtimeEntityType.AddAnnotation("Relational:SqlQuery", null);
    runtimeEntityType.AddAnnotation("Relational:TableName", null);
    runtimeEntityType.AddAnnotation("Relational:ViewDefinitionSql", null);
    runtimeEntityType.AddAnnotation("Relational:ViewName", "TestView");
    runtimeEntityType.AddAnnotation("Relational:ViewSchema", "dbo");

    Customize(runtimeEntityType);
}

As the source and entity variables appear to be redefined the code fails to compile.

Package: Microsoft.EntityFrameworkCore.Tasks 9.0.0-rc.2.24474.1

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions