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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:58:23+00:00 2026-05-17T00:58:23+00:00

Is there a way in Entity Framework 4 (using CTP4 and Code First if

  • 0

Is there a way in Entity Framework 4 (using CTP4 and Code First if that matters) to change the conventions used to automatically identify primary and foreign keys? I’m trying to use EF with a legacy database that uses a “pk/fk” prefix rather than an “id” suffix to mark keys. Also, it’s not uncommon to have multiple foreign keys to an Address table, for example, called “fkAddressHome” and “fkAddressWork”.

I would prefer a method for changing this behavior in a systematic way so that I don’t have to specify these changes for every table. I looked into the option of overriding the column names in the OnModelCreating method, but it was disappointing to see that if you add one column that way, then you have to add them all. And that violated my DRY sensibilities.

However, any solution would be appreciated. EF4, and particularly CTP4’s Code First enhancements, are really, really nice. I look forward to seeing what else makes into the next version of EF. Thanks for the help.

Update

I was able to map the relationship by adding the following to my OnModelCreating method:

modelBuilder.Entity<User>()
    .HasRequired<Address>(p => p.Address)
    .HasConstraint((p, u) => p.fkAddressHome == u.pkAddress);

I’m not sure if that’s the most elegant approach, or if this will ultimately cause me any problems, but it’s working for now.

  • 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-17T00:58:24+00:00Added an answer on May 17, 2026 at 12:58 am

    Fluent interface with Model Builder is a proper way to do that.
    You can also use DataAnnotation attributes (if you prefer attributes):

    http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx

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

Sidebar

Related Questions

Is there a way to make Entity Framework 4.1 Code First NOT to drop
I've tried using Entity Framework Code First and set AutomaticMigrationsEnabled to true so that
I am using Entity Framework 4.0, is there a way to change the sql
Using the default code generated entities with Entity Framework, is there a way to
Is there any way to validate using DataAnnotations in WPF & Entity Framework?
Background We have a WinForms application with Entity Framework 4.2 code-first / FluentAPI using
I'm writing an ASP.net MVC3 application using Entity Framework Code First and SqlCe4. I
I'm using Entity Framework 4.1's code first approach to map a class hierarchy to
I have a project using Entity Framework code first. For various reasons one of
Is there a way to insert data using LinqPad and the entity framework? You

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.