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

查看:205
本文介绍了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 ;-)

根据其他代码示例(此处)应该是可以将 _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'.

我在这里想念什么?

推荐答案

文档未反映实际规则,因为< camel -cased属性名称> (标准 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 流利的API进行操作,因为使用EF Core术语,导航属性不是属性,而是导航 。

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.

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

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(目前还没有专用的流利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天全站免登陆