EF Core 3:配置导航属性的支持字段 [英] EF Core 3: Configure backing field of navigation property

查看:32
本文介绍了EF Core 3:配置导航属性的支持字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下课程.它试图保护对 _assignedTrays 的访问.

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

实际上,它工作得很好,因为 EF 自动将支持字段 _assignedTrays 链接到属性 AssignedTrays - 按照约定(msdn)

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 ;-)

根据其他代码示例(here) 应该可以将 _assignedTrays 重命名为 assignedTrays 并在 OnModelCreating 中明确通知 EF:

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");
    });

但这给了我以下异常:

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?

推荐答案

文档没有反映实际规则,因为 (标准"C# 支持字段命名约定)绝对支持,甚至可能具有最高优先级.

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.

但是假设您的命名约定不受支持.您仍然可以映射支持字段,但不能使用 Property fluent API 来实现,因为根据 EF Core 术语,导航属性不是属性",而是导航".这适用于所有 fluent、更改跟踪等 API.

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.

为了配置导航,您需要访问关系构建器.然后您可以使用关联元数据的PrincipalToDependentDependentToPrrncipal 属性来访问/配置关系的两端.

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.

或者直接使用元数据 API(目前还没有专门的 fluent API).

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

例如:

modelBuilder.Entity<Rack>()
    .FindNavigation(nameof(Rack.AssignedTrays))
    .SetField("assignedTrays");

这篇关于EF Core 3:配置导航属性的支持字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆