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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:08:25+00:00 2026-05-15T15:08:25+00:00

Please help an EF n00b design his database. I have several companies that produce

  • 0

Please help an EF n00b design his database.
I have several companies that produce several products, so there’s a many-to-many relationship between companies and products. I have an intermediate table, Company_Product, that relates them.

Each company/product combination has a unique SKU. For example Acme widgets have SKU 123, but Omega widgets have SKU 456. I added the SKU as a field in the Company_Product intermediate table.

EF generated a model with a 1:* relationship between the company and Company_Product tables, and a 1:* relationship between the product and Company_Product tables. I really want a : relationship between company and product. But, most importantly, there’s no way to access the SKU directly from the model.

Do I need to put the SKU in its own table and write a join, or is there a better way?

  • 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-15T15:08:26+00:00Added an answer on May 15, 2026 at 3:08 pm

    I just tested this in a new VS2010 project (EFv4) to be sure, and here’s what I found:

    When your associative table in the middle (Company_Product) has ONLY the 2 foreign keys to the other tables (CompanyID and ProductID), then adding all 3 tables to the designer ends up modeling the many to many relationship. It doesn’t even generate a class for the Company_Product table. Each Company has a Products collection, and each Product has a Companies collection.

    However, if your associative table (Company_Product) has other fields (such as SKU, it’s own Primary Key, or other descriptive fields like dates, descriptions, etc), then the EF modeler will create a separate class, and it does what you’ve already seen.

    Having the class in the middle with 1:* relationships out to Company and Product is not a bad thing, and you can still get the data you want with some easy queries.

    // Get all products for Company with ID = 1
    var q =
        from compProd in context.Company_Product
        where compProd.CompanyID == 1
        select compProd.Product;
    

    True, it’s not as easy to just navigate the relationships of the model, when you already have your entity objects loaded, for instance, but that’s what a data layer is for. Encapsulate the queries that get the data you want. If you really want to get rid of that middle Company_Product class, and have the many-to-many directly represented in the class model, then you’ll have to strip down the Company_Product table to contain only the 2 foreign keys, and get rid of the SKU.

    Actually, I shouldn’t say you HAVE to do that…you might be able to do some edits in the designer and set it up this way anyway. I’ll give it a try and report back.

    UPDATE

    Keeping the SKU in the Company_Product table (meaning my EF model had 3 classes, not 2; it created the Company_Payload class, with a 1:* to the other 2 tables), I tried to add an association directly between Company and Product. The steps I followed were:

    • Right click on the Company class in the designer
    • Add > Association
    • Set “End” on the left to be Company (it should be already)
    • Set “End” on the right to Product
    • Change both multiplicities to “* (Many)”
    • The navigation properties should be named “Products” and “Companies”
    • Hit OK.
    • Right Click on the association in the model > click “Table Mapping”
    • Under “Add a table or view” select “Company_Product”
    • Map Company -> ID (on left) to CompanyID (on right)
    • Map Product -> ID (on left) to ProductID (on right)

    But, it doesn’t work. It gives this error:
    Error 3025: Problem in mapping fragments starting at line 175:Must specify mapping for all key properties (Company_Product.SKU) of table Company_Product.

    So that particular association is invalid, because it uses Company_Product as the table, but doesn’t map the SKU field to anything.

    Also, while I was researching this, I came across this “Best Practice” tidbit from the book Entity Framework 4.0 Recipies (note that for an association table with extra fields, besides to 2 FKs, they refer to the extra fields as the “payload”. In your case, SKU is the payload in Company_Product).

    Best Practice

    Unfortunately, a project
    that starts out with several,
    payload-free, many-to-many
    relationships often ends up with
    several, payload-rich, many-to-many
    relationships. Refactoring a model,
    especially late in the development
    cycle, to accommodate payloads in the
    many-to-many relationships can be
    tedious. Not only are additional
    entities introduced, but the queries
    and navigation patterns through the
    relationships change as well. Some
    developers argue that every
    many-to-many relationship should start
    off with some payload, typically a
    synthetic key, so the inevitable
    addition of more payload has
    significantly less impact on the
    project.

    So here’s the best practice.
    If you have a payload-free,
    many-to-many relationship and you
    think there is some chance that it may
    change over time to include a payload,
    start with an extra identity column in
    the link table. When you import the
    tables into your model, you will get
    two one-to-many relationships, which
    means the code you write and the model
    you have will be ready for any number
    of additional payload columns that
    come along as the project matures. The
    cost of an additional integer identity
    column is usually a pretty small price
    to pay to keep the model more
    flexible.

    (From Chapter 2. Entity Data Modeling Fundamentals, 2.4. Modeling a Many-to-Many Relationship with a Payload)

    Sounds like good advice. Especially since you already have a payload (SKU).

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

Sidebar

Related Questions

Help a sqlite n00b out, please. I have two tables CREATE TABLE students (
Please help me with a sanity check. Assuming a many-to-many relationship: Post, PostTagAssoc, Tag
Please help clarifying : In web.xml I have the following <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <el-ignored>false</el-ignored>
Please help! I'm on PHP. I have an url: ' http://example.com/articles/123a/view ', how do
please help me solve this issue. I have a client using WCF. I don't
please help with some basic syntax i need to make a statement that checks
Please help. I want to achieve that text and button on yellow box be
Please help me: create clickable regions in the canvas below that I can assign
Please help, where i work dont have a inhouse tech and they rely on
Please help me out with the following code. I don't understand it. I have

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.