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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:33:25+00:00 2026-06-10T15:33:25+00:00

I am trying to use code first with an existing db. So far it

  • 0

I am trying to use code first with an existing db. So far it went well, but now I am failing at a one to many to one relationship.

There is a table customer and a table address in the db where address does NOT have any customerid but a foreign one to many key FK_Address_Customer.

The auto created classes from the edmx look like

Customer:

public int CustomerID (PK)
public Address Address

Address:

public int AddressID (PK)
public HashSet<Customer> Customers

Whatever I do in the fluent API either fails with invalid column Address_AddressID or Multiplicity conflicts with the referential constraint error.

I assumed:

//Customer has one address, address can have multiple customers<br/>
pModelBuilder.Entity<Customer>().HasRequired(m => m.Address).WithMany(x => x.Customers);


//Address has multiple customers, customer has one address<br/>
pModelBuilder.Entity<Address>().HasMany(m => m.Customers).WithRequired();

I got this correct on other tables where there where NO foreign keys in the db by using HasForeignKey, but in the above scenario it does not work. I also tried via MapKey passing the foreign key name also with no luck.

Ho do I get this simple relationship going ?

Thanks

  • 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-10T15:33:26+00:00Added an answer on June 10, 2026 at 3:33 pm

    You must tell EF the name of FK in Customer table otherwise it will use default Address_AddressID. HasForeignKey will do the magic.

    pModelBuilder.Entity<Customer>()
                 .HasRequired(c => c.Address)
                 .WithMany(a => a.Customers)
                 .HasForeignKey(c => c.AddressID);
    

    Where AddressID is FK property in your Customer entity. If you don’t have FK property in the Customer entity you need to use Map:

    pModelBuilder.Entity<Customer>()
                 .HasRequired(c => c.Address)
                 .WithMany(a => a.Customers)
                 .Map(m => m.MapKey("AddressID"));
    

    Where AddressID is a name of FK column in Customer table. You always need that column to have one-to-many relation.

    The difference between having and not having FK property in your entity make difference between two types of associations supported by EF.

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

Sidebar

Related Questions

I am trying to use this code in my QT app QMap<QString,QMap> but there
I'm trying to use the Entity Framework code first without an existing database. I
I'm trying to use code-first on an existing Point-Of-Sale kiosk application. This is a
I'm trying to use the code review feature when I check-in my files but
I'm trying to use some code like this, and it's failing: data = Range(A1)
I'm trying to use Entity Framework code first to try to map a simple
I have some legacy code that I am now trying to re-use under Spring.
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
i am trying to use this code: <%= File.ReadAllText(Server.MapPath(Members/newsletters/welcome.html))%> which works great but now
I am trying to use the code from Microsoft for an Async Socket connection.

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.