EF Core 3: Configure backing field of navigation property

后端 未结 1 1486
面向向阳花
面向向阳花 2020-12-21 09:30

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

Actually, it works perfectly, since EF automatically links the backing f

相关标签:
1条回答
  • 2020-12-21 09:55

    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");
    
    0 讨论(0)
提交回复
热议问题