简体   繁体   English

反向导航阴影属性和代理

[英]Inverse navigation shadow properties and proxies

I'm encountering problems with EF Core 5.0 when I try to map a many-to-many relationship with a CLR navigation property on one end of the relationship only.当我尝试 map 仅在关系的一端与 CLR 导航属性建立多对多关系时,我遇到了 EF Core 5.0 的问题。

For example, a Question can have many Answers, and a single Answer can apply to many Questions (duplicates).例如,一个问题可以有多个答案,而一个答案可以应用于多个问题(重复)。 To keep things simple, I don't want to navigate back from an answer to the duplicate questions.为了简单起见,我不想从重复问题的答案中返回。

public class Question
{
    public int Id { get; set; }

    public virtual IEnumerable<Answer> Answers { get; set; } = new List<Answer>();

    public string Text { get; set; } = "";
}

public class Answer
{
    public int Id { get; set; }

    public string Text { get; set; } = "";
}

public class SomeDbContext : DbContext
{
    public DbSet<Question> Questions => Set<Question>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Question>().HasMany(q => q.Answers).WithMany("Duplicates");
    }
}

This works fine, until I enable change tracking proxies or lazy-loading proxies.这工作正常,直到我启用更改跟踪代理或延迟加载代理。 Then I start getting exceptions that say I need to add an inverse navigation property.然后我开始收到说我需要添加反向导航属性的异常。

System.InvalidOperationException: Property 'Duplicates' on entity type 'Answer' is mapped without a CLR property. System.InvalidOperationException:实体类型“答案”上的属性“重复”在没有 CLR 属性的情况下映射。 'UseChangeTrackingProxies' requires all entity types to be public, unsealed, have virtual properties, and have a public or protected constructor. “UseChangeTrackingProxies”要求所有实体类型都是公共的、未密封的、具有虚拟属性并具有公共或受保护的构造函数。 'UseLazyLoadingProxies' requires only the navigation properties be virtual. “UseLazyLoadingProxies”只要求导航属性是虚拟的。

I understand that proxies require virtual navigation properties in order to add the desired behavior, but is there no way to do this with shadow properties?我知道代理需要virtual导航属性才能添加所需的行为,但是有没有办法用影子属性来做到这一点?

I understand that proxies require virtual navigation properties in order to add the desired behavior, but is there no way to do this with shadow properties?我知道代理需要virtual导航属性才能添加所需的行为,但是有没有办法用影子属性来做到这一点?

Currently (up to v6.0 inclusive) EF Core does not support shadow navigation properties.目前(包括 v6.0)EF Core支持阴影导航属性。

I'm encountering problems with EF Core 5.0 when I try to map a many-to-many relationship with a CLR navigation property on one end of the relationship only.当我尝试 map 仅在关系的一端与 CLR 导航属性建立多对多关系时,我遇到了 EF Core 5.0 的问题。

As explicitly stated at the beginning of the Many-to-many documentation:正如多对多文档开头明确指出的那样:

Many-to-many relationships require a collection navigation property on both sides.多对多关系需要双方都有一个集合导航属性。

There are plans to add support for single side skip navigation (unidirectional) many-to-many relationships in the future, but currently this is a limitation (and requirement for your model).有计划在未来添加对单侧跳过导航(单向)多对多关系的支持,但目前这是一个限制(和您的模型的要求)。

This works fine, until I enable change tracking proxies or lazy-loading proxies.这工作正常,直到我启用更改跟踪代理或延迟加载代理。

As mentioned above, what you are doing is not supported.如上所述,不支持您正在执行的操作。 You just found a backdoor in 5.0 which btw is closed in 6.0, so in 6.0 such model configuration simply throws InvalidOperationException saying您刚刚在 5.0 中发现了一个后门,顺便说一句在 6.0 中关闭,所以在 6.0 中,这样的 model 配置只会抛出InvalidOperationException

Unable to set up a many-to-many relationship between 'Answer.Duplicates' and 'Question.Answers' because one or both of the navigations don't have a corresponding CLR property.无法在“Answer.Duplicates”和“Question.Answers”之间建立多对多关系,因为其中一个或两个导航没有对应的 CLR 属性。 Consider adding a corresponding private property to the entity CLR type.考虑将相应的私有属性添加到实体 CLR 类型。

even without tracking or lazy loading proxies.即使没有跟踪或延迟加载代理。

Shortly, whether you want it or not, the proper way until they add support for what you are asking for is to simply follow their requirements and put collection navigation properties on both sides.简而言之,无论您是否愿意,在他们为您所要求的内容添加支持之前,正确的方法是简单地遵循他们的要求并将集合导航属性放在两侧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM