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

  • Home
  • SEARCH
  • 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 8683869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:09:20+00:00 2026-06-12T22:09:20+00:00

I am working with Entity Framework to retrive blog data from a database. I

  • 0

I am working with Entity Framework to retrive blog data from a database. I have a class “BlogPost” and a class “Author”. One Author may have many blog posts, but the Author object itself does not contain a list of them. As far as the Author object is concerned, it is independent of all blog posts.

An instance of the Author class is included as a property in the BlogPost. Every author has an AuthorId. I would like to pull from my database a given blog and have the Author object filled with the pertinent data. I have managed to do this by including an AuthorId property in my BlogPost class, using the following code to map the object:

this.HasRequired(t => t.Author).WithMany().HasForeignKey(x => x.AuthorId);

What I would like to do, however, is to be able to do the same thing but without having to include the AuthorId property directly in the model. The reason being, is because as the Author object contains the coresponding ID already, I am repeating the same piece of information twice. I would like to do something along these lines:

this.Property(t => t.Author.Id).HasColumnName("id_user");
this.HasRequired<TwitterUser>(t => t.Author).WithMany().HasForeignKey(t => t.Author.Id);

So, is there any way do retrieve the Author sub-object without having to store the AuthorID redundantly in the BlogPost object alongside the Author object itself?

  • 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-06-12T22:09:21+00:00Added an answer on June 12, 2026 at 10:09 pm

    The syntax you are using is called “foreign key associations”, and was introduced in EF 4 because it solves a number of problems. For example, when using FK Associations you can perform cascaded deletes automatically.

    You can read more about them here:

    http://blogs.msdn.com/b/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx

    You can still do things the old way, with simple navigational properties. But you will need to a slightly different syntax. One thing is that independent associations require that you have navigational properties on both ends, and you said you only wanted this one-way. So you are stuck either adding an association to your Author entity, or using FK associations.

    You may also find this article by our own Ladislav Mrnka useful:

    http://www.ladislavmrnka.com/2011/05/foreign-key-vs-independent-associations-in-ef-4/

    Also note, that according to the EF team, Independent Associations can perform much worse than FK Associations, although it seems this is limited to View Generation, so it’s a one-time startup cost (each time you start the app or app domain). Also, for small models you probably won’t notice it.:

    http://msdn.microsoft.com/en-us/data/hh949853.aspx

    EDIT:

    As was pointed out by @Slauma, you can map an independent association by using the .Map property like so:

    this.HasRequired(t => t.Author).WithMany().Map(m => m.MapKey("AuthorId"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a change analyzer for Entity Framework and have one small
I am working with Entity Framework ... I have a database table for Patient
I'm changing my project from working with Linq-to-SQL to working with Entity Framework. I
I'm working with a small model in Entity Framework 4.0. I'd like to have
I'm working on an import from a CSV file to my ASP.NET MVC3/C#/Entity Framework
Does WCF Data Services support working with Entity Framework Code First (4.1)? It seems
I have been working with entity framework and ASP MVC for a while. I
I have been working with NHibernate, LINQ to SQL, and Entity Framework for quite
I have an application I am working on, using Entity Framework 4.1 as my
We're working with Entity Framework 4 but we're being told the database to use

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.