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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:23:49+00:00 2026-06-07T01:23:49+00:00

Given the standard Northwind database (reduced for conciseness): [Table(Order Details)] public class OrderDetail :

  • 0

Given the standard Northwind database (reduced for conciseness):

   [Table("Order Details")]
    public class OrderDetail : IValidatableObject
    {
        [Key, Column(Order = 1)]
        public int OrderID { get; set; }
        [Key, Column(Order = 2)]
        public int ProductID { get; set; }
        ...
    }
   [Table("Orders")]
    public class Order
        {
        public int OrderID { get; set; }
        ...
        public virtual ICollection<OrderDetail> OrderDetails { get; set; }
    }

I’m wondering about how this works exactly. I’m guessing that because the primary key in Order (OrderID) has foreign key in OrderDetails (OrderID) that is named the same, EF’s rules correctly guess that they have a foreign-key/primary key relationship.

Is that correct?

Part two is another question:

I have an existing database where the foreign key is not named the same (change OrderId in OrderDetail to RecordNumber, for example – (I can’t change the database)). In this case, I’m struggling trying to understand how to relate the two tables.

I’m using the Fluent API (and not the annotations as in above) and have something like this:

---OrderDetails

// set up the primary key
HasKey(t => new { t.RecordNumber, t.ProductID };

---Order

HasKey(t => t.OrderId);

HasRequired(t => t.OrderDetails).WithMany().HasForeignKey(t => t.OrderId)

(The last statement seems incredibly wrong, in that the only thing that HasForeignKey(…) seems to be able to relate to is the declared primary composite key)

I’ve also tried

HasMany((t => t.OrderDetails).WithMany()

which would seem to be different from the prior statement in that it makes it possible to have no OrderDetails.

Neither works.

The idea I am trying to express is that an order must have at least one OrderDetails – and I’m trying to be able to navigate thusly:

   Con.Orders.Single(o => o.OrderId == 1).OrderDetails

The problem I am having is that the foreign key I declared with HasForeignKey(…) does not relates to anything on Orders (and the code throws an appropriate exception – “Multiplicity is not valid”), and I am left not knowing how to accomplish my goal.

Any thoughts? I appreciate your help in advance.

  • 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-07T01:23:50+00:00Added an answer on June 7, 2026 at 1:23 am

    I’m guessing that because the primary key in Order (OrderID) has
    foreign key in OrderDetails (OrderID) that is named the same, EF’s
    rules correctly guess that they have a foreign-key/primary key
    relationship.

    Yes, correct. The important part though is that Order has a navigation collection to OrderDetails. EF infers from this property (and the fact that there is no corresponding navigation property in OrderDetail refering to Order) that there is a one-to-many relationship between the two entities. Then the foreign key property is infered by naming conventions.

    The idea I am trying to express is that an order must have at least
    one OrderDetails.

    You can’t define such a constraint in the mapping between model and database. You must ensure this in your application’s business logic. In a one-to-many relationship the collection can have zero elements.

    The correct mapping with Fluent API would be:

    modelBuilder.Entity<Order>()
        .HasMany(order => order.OrderDetails)
        .WithRequired()
        .HasForeignKey(orderDetail => orderDetail.RecordNumber);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a class like this: class Foo { public: Foo(int); Foo(const Foo&); Foo& operator=(int);
Given a table foo with a composite primary key (a,b) , is there a
Given that I have a Foo model w/ the standard Rails timestamp columns what
Given DateTime start = startsomething and DateTime end = endSomething Is there a standard
Seems like a standard approach for an ioc when given a scenario like (C#
given a Python class hierarchy, say class Base: def method1 def method2 def method3
Given a MySQL table of real estate data, I would like to generate a
given a standard model (called Image) with an autoset 'id', how do I get
Standard equation for generating DCT coefficients in JPEG compression process is DCT formula given
Is there any facility in the standard Java libraries that, given a CharSequence, produces

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.