Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4340440
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:20:57+00:00 2026-05-21T11:20:57+00:00

All my models contain at least two associations. When modeling this in ef4 I’ve

  • 0

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; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:20:58+00:00Added an answer on May 21, 2026 at 11:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to, when given a particular model, return all the related models it
I've models for Books , Chapters and Pages . They are all written by
All I want is to be able to change the color of a bullet
If I have some classes that implement the same interface, so they all contain
I'm using Ubuntu 10, python 2.6.5 I'm following this tutorial: http://www.djangobook.com/en/2.0/chapter02 I followed all
I have the next question. There is part of code: $result = $this->find('all', array(
How do you model associations without running into the N+1 selects problem or inconsistent
I've been modeling many to many relationships in gae using the classic ListProperty of
I'm trying to link two (and more) models in a CakePHP application. I have
Let's assume we have two models: User and Token . A user may have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.