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

The Archive Base Latest Questions

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

I have a quick question about setting up the mappings between [Order Details] ,

  • 0

I have a quick question about setting up the mappings between [Order Details], [Products] and [Orders] in the Northwind datbase.

[Order Details] has no primary key, and looks like this

[Order Details]
OrderId (int)
ProductId (int)
...

So my question is how do I (and can I) set up my OrderDetails class to work like this?

public class OrderDetails
{
    public virtual Order Order { get; set; }
    public virtual Product Product { get; set; }
    public Decimal UnitPrice { get; set; }
    public int Quantity { get; set; }
    public Decimal Discount { get; set; }
}

My data context looks like this

public class NorthwindDb : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderDetails> OrderDetails { get; set; }
    public DbSet<Customer> Customers { get; set; }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new OrderDetailsConfiguration());
    }

    public static void InitializeBecauseOfThatWeirdMetaDataThingThatIDontUnderstandYet()
    {
        Database.SetInitializer<NorthwindDb>(null);
    }

}

And My OrderDetailsConfiguration (empty because I don’t know what I’m doing)

public class OrderDetailsConfiguration : EntityConfiguration<OrderDetails>
{
    public OrderDetailsConfiguration()
    {
        //HasKey(x => x.Order.OrderId);
        //HasKey(x => x.Product.ProductId);
    }
}

Any hints or ideas would be great.

  • 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-18T00:23:54+00:00Added an answer on May 18, 2026 at 12:23 am

    First you would have to explicitly put in the PKs and FKs inside OrderDetails class:

    public class OrderDetails 
    {        
        public int OrderID { get; set; }
        public int ProductID { get; set; }
    
        public virtual Order Order { get; set; }
        public virtual Product Product { get; set; }
    
        public Decimal UnitPrice { get; set; }
        public int Quantity { get; set; }
        public Decimal Discount { get; set; }
    }
    

    Then with the following code you can specify the PKs for OrderDetailsConfiguration class:

    (Please note that in Northwind database both OrderID and ProductID are PKs for OrderDetails table).

    public class OrderDetailsConfiguration : EntityConfiguration<OrderDetails> 
    {
        public OrderDetailsConfiguration() 
        {
            HasKey(od => new 
            {
                od.ProductID,
                od.OrderID
            });
        }
    }
    

    And also you can use RecreateDatabaseIfModelChanges strategy to force your database to be recreated every time you change the class model:

    Database.SetInitializer<NorthwindDb>(
            new RecreateDatabaseIfModelChanges<NorthwindDb>());
    

    The interesting point is that once you made these changes EF will automatically infer the FKs from OrderDetails class and create the relationships to Orders and Products table on OrderID and ProductID based on the Conventions for Code First:

    Code First will infer that any property named <navigation property name><primary key property name> (i.e. ProductsProductID), <principal class name><primary key property name> (i.e. ProductProductID) or <primary key property name> (i.e. ProductID), with the same data type as the primary key, represents a foreign key for the relationship. If multiple matches are found then precedence is given in the order listed above. Foreign key detection will not be case sensitive.

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

Sidebar

Related Questions

I have a quick question about programming using Vim. I sometimes make a silly
i have a quick Binding Question about Silverlight. I have some Expander and want
I'm diving into iOS development and I have a quick noob question about Core
A quick question about class design. I'm fairly new to Software Development and wondered
Just a quick question. I can get a form to show with other exceptions,
got a quick question for you (pretty much the title): is the book Cocoa
Quick question, is a jQuery child selector without a parent ever valid? If so,
I've built a PHP AJAX pagination that works with an HTML Table. I have
I am frequently making plots for my own research and all of the default

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.