导航属性不宣外键 [英] Navigation Property without Declaring Foreign Key

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

问题描述

我所有的模型包含至少两个关联。当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的映射惯例假设修改是一体的两端,相同的关系,但不能确定哪些主体,什么依赖该协会是。至于我可以在支持的属性列表中看到没有选项定义本金和相关的一端与数据的注解。

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.

除此之外,我猜你真正想要的两个关系的,都与未在模型中暴露的结束。这意味着你的模式是非常规相对于映射约定。 (我认为修改之间的关系实际上是无稽之谈 - 从语义的观点。)

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

所以,流利的API,你想这样的:

So, in Fluent API, you want this:

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

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

由于用户可以是许多其他用户记录的创造者或修改。对吧?

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

如果你想创建一个不流利的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; }
}

我这里假设修改是必需的,否则我们可以省略 [必需] 属性。

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

我认为这是在哪里使用流利的API使得有很大的意义,比修改模型只是为了避免流利的配置更好的明确的情况下。

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.

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

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