本文介绍了不声明外键的导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所有的模型都至少包含两个关联.在 ef4 中建模时,我只能通过使用流畅的界面在没有第二个外键属性的情况下做到这一点.ForeignKey 似乎是使用正确的属性,除了它需要一个字符串参数的事实.

All my models contain at least two associations. When modeling this in ef4 I've only been able to do this without a second Foreign Key property through the use of the fluent interface. ForeignKey seems like the right attribute to use, except for the fact that it requires a string parameter.

所以我的问题是,您可以拥有导航属性并使用属性声明它吗?

So my question is, can you have a navigational property and declare it as such using an attribute?

public class User : IAuditable
{
    // other code

    public virtual User Creator { get; set; }

    public virtual User Modifier { get; set; }
}

推荐答案

我相信,仅用数据属性来定义关系是不可能的.问题是EF的映射约定假设CreatorModifier是同一个关系的两端,却无法确定这个关联的主体和依赖是什么.就我在支持的属性列表中看到的而言,没有选项可以使用数据注释定义主体和从属端.

I believe, it is not possible to define the relationship only with data attributes. The problem is that EF's mapping conventions assume that Creator and Modifier are the two ends of one and the same relationship but cannot determine what the principal and what the dependent of this association is. As far as I can see in the list of supported attributes there is no option to define principal and dependent end with data annotations.

除此之外,我猜您实际上想要两种关系,它们的末端都没有在模型中公开.这意味着您的模型在映射约定方面是非常规的".(我认为 CreatorModifier 之间的关系实际上是无稽之谈 - 从语义的角度来看.)

Apart from that, I guess that you actually want two relationships, both with an end which isn't exposed in the model. This means that your model is "unconventional" with respect to the mapping conventions. (I think a relationship between Creator and Modifier is actually nonsense - from a semantic viewpoint.)

因此,在 Fluent API 中,您需要:

So, in Fluent API, you want this:

modelBuilder.Entity<User>()
            .HasRequired(u => u.Creator)
            .WithMany();

modelBuilder.Entity<User>()
            .HasRequired(u => u.Modifier)
            .WithMany();

因为 User 可以是许多其他用户记录的创建者或修改者.对吗?

Because a User can be the Creator or Modifier of many other User records. Right?

如果您想在没有 Fluent API 的情况下仅使用 DataAnnotations 创建这两种关系,我认为您必须将关联的多端引入您的模型,如下所示:

If you want to create these two relationships without Fluent API and only with DataAnnotations I think you have to introduce the Many-Ends of the associations into your model, like so:

public class User
{
    public int UserId { get; set; }

    [InverseProperty("Creator")]
    public virtual ICollection<User> CreatedUsers { get; set; }
    [InverseProperty("Modifier")]
    public virtual ICollection<User> ModifiedUsers { get; set; }

    [Required]
    public virtual User Creator { get; set; }
    [Required]
    public virtual User Modifier { get; set; }
}

这里假设 CreatorModifier 是必需的,否则我们可以省略 [Required] 属性.

I assume here that Creator and Modifier are required, otherwise we can omit the [Required] attribute.

我认为这是一个明显的例子,使用 Fluent API 很有意义,并且比仅仅为了避免 Fluent 配置而修改模型要好.

I think it's a clear case where using the Fluent API makes a lot of sense and is better than modifying the model just to avoid Fluent configuration.

这篇关于不声明外键的导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 22:14
查看更多