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 4611450
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:16:10+00:00 2026-05-22T01:16:10+00:00

Using EF 4.0 with the 4.1 upgrade for POCO / code-first. OK so I

  • 0

Using EF 4.0 with the 4.1 upgrade for POCO / code-first.

OK so I have a domain model where type Car has, in a collection, multiple objects of type Part. So a one:many relation.

HasMany(v => v.Parts)
  .WithRequired()
  .HasForeignKey(v => v.CarId)
  .WillCascadeOnDelete();

The problem with this is that it requires me to add a CarId property to my Part type. This is leaking ORM detail into my domain model – which is bad. Marking everything virtual is annoying enough.

Looking at the XML doc comment for the HasForeignKey() method says this:

Configures the relationship to use
foreign key property(s) that are
exposed in the object model. If the
foreign key property(s) are not
exposed in the object model then use
the Map method.

That’s great and all. But it introduces a catch-22 situation because if I refactor my Part type by removing the CarId property that I don’t want and update my EF model builder to not bother with mapping that property. Then as you can imagine it means I cannot then call HasKey() for defining the composite key, ala:

HasKey(v => new { v.CarId, v.PartId });

HasKey() doesn’t appear to support defining the keys based upon non-Property lambdas.

What is the solution here?

  • 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-22T01:16:11+00:00Added an answer on May 22, 2026 at 1:16 am

    If you absolutely don’t like to have foreign key properties in your model you could remove the convention to detect FK properties to avoid that EF marks properties automatically as FK properties …

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions
            .Remove<NavigationPropertyNameForeignKeyDiscoveryConvention>();
    }
    

    … and then simply don’t specify the FK property in your mapping:

    HasMany(v => v.Parts)
        .WithRequired()
        .WillCascadeOnDelete();
    

    You still need CarId in your model because it is part of the primary key, but this way it doesn’t act anymore as foreign key property.

    Just an idea, I am not sure if it works.

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

Sidebar

Related Questions

I have a chance to upgrade a legacy suite of applications currently using .net
I have upgrade my MVC2 project to MVC3 using this tool: http://blogs.msdn.com/b/marcinon/archive/2011/01/13/mvc-3-project-upgrade-tool.aspx Which is
I have upgrade the project from framework3.5 to framework4.0.Right Now i am using Visual
I am using EF4.1, have upgraded my project, generated POCO classes to use DbContext
I have this crash issue after I upgrade using XCode 4.2. It never happened
We have been using Flurry 2.8.4 and wish to upgrade to 3.0.0 Anyone know
I am trying to upgrade my code from using Crystal Reports 11 to Crystal
I have attempted to upgrade Opencart 1.4.8 with custom theme using update script but
I'm using ASP.NET Preview 5 (will upgrade soon to Beta) with LINQ2SQL on my
I've started using Visual Studio 2008 and it keeps asking me to upgrade my

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.