Efcore: Duplicate backing field is throwing even with HasField correction

Created on 7 Sep 2019  路  3Comments  路  Source: dotnet/efcore

I am testing out this use case for https://github.com/aspnet/EntityFrameworkCore/pull/14735 but it is throwing even with the explicit mapping.

In my class I have:

   private string _name;
    public string Name => _name;
    private string m_name;
    public string OwnerName => m_name;

In context:

    modelBuilder.Entity<Company>().Property(c=>c.Name).HasField("_name");
     modelBuilder.Entity<Company>().Property(c=>c.OwnerName).HasField("m_name");

When debugging, it is throwing the new exception when it hits the first configuration line (hasfield for Name) ...ambiguous fields, please use HasField().
image

I changed the order so that OwnerName/m_name mapping is called first but that didn't help. I have cleaned. deleted bin/obj, rebuilt etc. And stared at my code to see if I'm doing something lame brained. (Which surely will spotted instantly by someone else but I have to take my chances ;) )

package being pulled in is: 3.0.0-preview9.19423.6

Exception
Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code: 'Property 'Name' on entity type 'Company' matches both '_name' and 'm_name' by convention. Explicitly specify the backing field to use with '.HasField()' in 'OnModelCreating()'.'
at Microsoft.EntityFrameworkCore.Metadata.Conventions.BackingFieldConvention.TryMatch(KeyValuePair2[] array, String prefix, String middle, String suffix, TypeInfo typeInfo, FieldInfo existingMatch, Type entityClrType, String propertyName) at Microsoft.EntityFrameworkCore.Metadata.Conventions.BackingFieldConvention.TryMatchFieldName(IConventionModel model, Type entityClrType, Type propertyType, String propertyName) at Microsoft.EntityFrameworkCore.Metadata.Conventions.BackingFieldConvention.GetFieldToSet(IConventionPropertyBase propertyBase) at Microsoft.EntityFrameworkCore.Metadata.Conventions.BackingFieldConvention.ProcessPropertyAdded(IConventionPropertyBuilder propertyBuilder, IConventionContext1 context)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnPropertyAdded(IConventionPropertyBuilder propertyBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.RunVisitor.VisitOnPropertyAdded(OnPropertyAddedNode node)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnPropertyAddedNode.Accept(ConventionVisitor visitor)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionVisitor.Visit(ConventionNode node)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionVisitor.VisitConventionScope(ConventionScope node)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionBatch.Run()
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionBatch.Dispose()
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Property(Type propertyType, String propertyName, MemberInfo memberInfo, Nullable1 typeConfigurationSource, Nullable1 configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Property(MemberInfo memberInfo, Nullable1 configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder1.PropertyTProperty

closed-fixed customer-reported type-bug

All 3 comments

Arggh ..one minute later I had a new idea. ONLY map the OwnerName field because that's NEEDED anyway since there's nothing conventional about it's field name. And don't map the Name field.
But I think it's not out of the question to want to explicitly map both fields. Sorry for the last minute discovery. Also, I hope nobody every really writes code like this anyway. :)

Convention shouldn't be throwing exception like that.

Yay!

Was this page helpful?
0 / 5 - 0 ratings