31

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; }
}
Matt
  • 74,352
  • 26
  • 153
  • 180
Daniel Little
  • 16,975
  • 12
  • 69
  • 93

1 Answers1

36

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

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?

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.

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.

Slauma
  • 175,098
  • 59
  • 401
  • 420
  • +1 really good answer. I didn't know about `InverseProperty` attribute – Ladislav Mrnka Apr 17 '11 at 13:57
  • Nice answer, I was wondering about the inverse attribute, It's a shame it's not possible atm tho. Do you think it's possible to generate the mappings based on a custom attribute? – Daniel Little Apr 17 '11 at 23:21
  • @Lavinski: If you define your own custom attribute I think you would need to evaluate that using Reflection and then based on this evaluation create the mapping with the Fluent API. It may be worth the effort if you have really many of those classes which would require this custom attribute. Otherwise I'd go with the Fluent API and don't care about attributes. There are many mapping configurations which are not possible with attributes and only with Fluent code. So using the Fluent API is often unavoidable anyway. – Slauma Apr 18 '11 at 11:13
  • @Lavinski: Attributes are processed by conventions and custom conventions cannot be added at this version. – Ladislav Mrnka Apr 19 '11 at 12:54