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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:49:22+00:00 2026-05-25T17:49:22+00:00

I am having my first steps in EF 4.1. Because I was using NHibenate,

  • 0

I am having my first steps in EF 4.1. Because I was using NHibenate, the code first approach seems to me as the best one. I have problem with good mapping of one-to-many (or many-to-one) realtionship. Let’s say I have 2 entities:

class ClientModel
{
    int ClientID;
    string Name;
    virtual IList<OrderModel> Orders;
}

class OrderModel
{
    int OrderID;
    string Details;
    virtual ClienModel Client;
}

When I leave it like that, there is an error while generating database – keys in tables are missing. I figured out I can fix it by changing names of the keys to ID (but it’s not OK with my naming convention) or by adding [Key] annotation. Even if I add this annotation, still the names of tables are wrong – just like classes names but with ‘s’.
So I tried to use fluent API – I made mappings. But if I set mappings just like here:

class ClientMapping
{
    ClientMapping()
    {
        this.HasKey(e => e.ClientID).Property(e => e.ID).HasColumnName("ClientID");
        this.Property(e => e.Name).HasColumnName("Name");
        this.HasMany(e => e.Orders).WithOptional().Map(p => p.MapKey("OrderID")).WillCascadeOnDelete();
        this.ToTable("Clients");
    }
}

class OrderMapping
{
    OrderMapping()
    {
        this.HasKey(e => e.OrderID).Property(e => e.OrderID).HasColumnName("OrderID");
        this.Property(e => e.Details).HasColumnName("Details");
        this.HasRequired(e => e.Client).WithMany().Map(p=>p.MapKey("Client")).WillCascadeOnDelete(false);
        this.ToTable("Orders");
    }
}

the relation betweene tables in database is doubled.
What is the proper way to do one-to-many relationship using code-first approach? Am I thinking in a good direction or is it a wrong approach?

EDIT

OK, I have done it in the way @Eranga showed, but there is still a problem. When I’m getting Client from database, its Orders property is null (but in database it has some Orders with Order.ClientID == Client.ClientID).

  • 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-25T17:49:23+00:00Added an answer on May 25, 2026 at 5:49 pm

    You need to map both properties participating in the relationship. You need to add ClientID column to Orders table.

    class ClientMapping
    {
        ClientMapping()
        {
            this.HasKey(e => e.ClientID).Property(e => e.ID).HasColumnName("ClientID");
            this.Property(e => e.Name).HasColumnName("Name");
    
            this.HasMany(e => e.Orders).WithRequired(o => o.Client)
               .Map(p => p.MapKey("ClientID")).WillCascadeOnDelete();
    
            this.ToTable("Clients");
        }
    }
    
    class OrderMapping
    {
        OrderMapping()
        {
            this.HasKey(e => e.OrderID).Property(e => e.OrderID).HasColumnName("OrderID");
            this.Property(e => e.Details).HasColumnName("Details");
            this.ToTable("Orders");
        }
    }
    

    Configuring the relationship from one entity is sufficient.

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

Sidebar

Related Questions

I am having an problem. First time working with a windows server, do you
I am currently trying to solve a problem, that I have using Hibernate 4
I really like the Entity Framework Code First idea because I can rapidly prototype
I'm having a problem with Chrome trying to set an opacity below 1 because
I'm working on oop c++ with code::Blocks. These are my first steps in oop
I'm having a problem using Git. Bear in mind I'm very new to Git.
OK, having tried my first TDD attempt, it's time to reflect a little and
Having just read the first four chapters of Refactoring: Improving the Design of Existing
First time cake user and I'm having real apache problems. For some reason the
Having recently discovered design patterns, and having acquired the excellent Head First Design Patterns

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.