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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:30:23+00:00 2026-05-26T09:30:23+00:00

Sorry if this is a simple thing, but I’m just learn EF and can’t

  • 0

Sorry if this is a simple thing, but I’m just learn EF and can’t seem to find my answer (or maybe it’s just understand what I’m finding).

I have three existing database tables as follows:

Order:
   int OrderNo (Primary Key generated by program)
   ...

OrderDetails:
   int OrderNo (Foreign key from Order=>OrderNo; but part of primary key for OrderDetails)
   int DetailNo (Part of primary key; generated by program)
   ...

OrderResults:
   int OrderNo (Key from Detail=>OrderNo; but part of primary key for OrderResults)
   int DetailNo (Key from Detail=>DetailNo; but part of primary key fro OrderResult)
   int ResultNo (Part of primary key; generated by program)
   ...

My Entities are defined as:

public Class Order
{
   public int OrderNo { get; set; }

   publice virtual ICollection<Detail> Details { get; set; }
}

public Class Detail
{
   public int OrderNo { get; set; }
   public int DetailNo { get; set; }

   public virtual Order Order { get; set; }
   public virtual ICollection<Result> Results { get; set; }
}

public Class Result
{
   public int OrderNo { get; set; }
   public int DetailNo { get; set; }
   public int ResultNo { get; set; }

   public virtual Detail Detail { get; set; }
}

So how would you define these key and associations using the fluent API? Currently I’m getting the OrderDetails linked to the Order correctly, but not getting the correct OrderResults.

  • 1 1 Answer
  • 1 View
  • 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-26T09:30:24+00:00Added an answer on May 26, 2026 at 9:30 am

    You can try something like this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Mapping for Order
        modelBuilder.Entity<Order>()
            .HasKey(o => o.OrderNo);
    
        modelBuilder.Entity<Order>()
            .Property(o => o.OrderNo)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
        modelBuilder.Entity<Order>()
            .HasMany(o => o.Details)
            .WithRequired(d => d.Order)
            .HasForeignKey(d => d.OrderNo);
    
        // Mapping for Detail
        modelBuilder.Entity<Detail>()
            .HasKey(d => new { d.OrderNo, d.DetailNo });
        // I believe, setting to DatabaseGeneratedOption.None is not necessary
        // for composite keys
    
        modelBuilder.Entity<Detail>()
            .HasMany(d => d.Results)
            .WithRequired(r => r.Detail)
            .HasForeignKey(r => new { r.OrderNo, r.DetailNo });
    
        // Mapping for Result
        modelBuilder.Entity<Result>()
            .HasKey(r => new { r.OrderNo, r.DetailNo, r.ResultNo });
    }
    

    Fluent API is quite verbose in this example. You can all the mappings also define with data annotations.

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

Sidebar

Related Questions

Sorry if this is a simple question, but I just can't figure out what
sorry this is such a simple question but I can't figure it out. How
Sorry, I'm sure this is simple but I'm tired and can't figure it out.
Sorry if this is probably a simple fix but I can't think of one.
Sorry if this is too simple, but thanks in advance for helping. This is
Sorry of this appears a bit simple a question, but is there a way
Sorry if this is a dead simple question but I'm confused from the documentation
Sorry this is a basic question, but all my research just barely missed answering
Sorry for this simple questions but i am too much confused with how to
Sorry for what is probably a simple thing, but not for me... I need

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.