EF Core 3: Configure backing field of navigation property
Asked Answered
S

1

6

Consider the following class. It tries to protect the access to the _assignedTrays.

Actually, it works perfectly, since EF automatically links the backing field _assignedTrays to the property AssignedTrays - by convention (msdn)

    public class Rack
    {
        private List<Tray> _assignedTrays = new List<Tray>();

        private Rack()
        {
        }

        public Rack(string rackId)
        {
            this.Id = rackId;
        }

        public string Id { get; private set; }

        public IReadOnlyList<Tray> AssignedTrays => this._assignedTrays.AsReadOnly();

        public void Assign(params Tray[] trays)
        {
            this._assignedTrays.AddRange(trays);
        }
    }

The problem is, that our coding styles forbid the use of underscores in variable names ;-)

According to other code examples (here) it should be possible to just rename _assignedTrays to assignedTrays and just explicitly inform EF about that change in the OnModelCreating:

    modelBuilder.Entity<Rack>(e =>
    {
        e.Property(t => t.AssignedTrays).HasField("assignedTrays");
    });

But that gives me the following exception:

System.InvalidOperationException: The property 'Rack.AssignedTrays' is of type 'IReadOnlyList<Tray>' which 
is not supported by current database provider. Either change the property CLR type or ignore the property
using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

What am I missing here? Shouldn't it work?

Skimpy answered 10/3, 2020 at 12:14 Comment(5)
I don't think there's anything you can do really. The workaround would be to rename the backing field, for example internalAssignedTrays and add a comment as to why you can't call it something else so it will pass a code review.Sharecrop
I could be mapped differently (Property method is not for navigation properties), but there is no need to, since EF Core convention supports your naming style.Nepheline
@IvanStoev But the convention doesn't allow for this convention without the underscores.Sharecrop
@Sharecrop Oops, the documentation seems to be incomplete, <camel-cased property name> (the default C# backing field naming convention) is also supported - probably with highest priority.Nepheline
@IvanStoev: You're right. I wondered why the integration tests turned green when not using the HasField configuration, but I blamed it on 'incomplete tests'...Skimpy
N
3

The documentation does not reflect the actual rules, because <camel-cased property name> (the "standard" C# backing field naming convention) is definitely supported, and probably even with highest priority.

But let say your naming convention is not supported. You can still map the backing field, but you can't do that with Property fluent API, because by EF Core terminology navigation properties are not "properties", but "navigations". This applies to all fluent, change tracking etc. APIs.

In order to configure navigation, you need to get access to the relationship builder. Then you can use the PrincipalToDependent and DependentToPrrncipal properties of the associated metadata to access/configure the two ends of the relationship.

Or use directly the metadata APIs (currently there is no dedicated fluent API for that anyway).

For instance:

modelBuilder.Entity<Rack>()
    .FindNavigation(nameof(Rack.AssignedTrays))
    .SetField("assignedTrays");
Nepheline answered 10/3, 2020 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.