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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:11:01+00:00 2026-05-26T15:11:01+00:00

I’m not sure how to map the following tables below in EF 4.1 code

  • 0

I’m not sure how to map the following tables below in EF 4.1 code first and what objects I need representing the tables. How would I retrieve a list of the product’s specifications?

I currently only have a Product class.

Products Table:
Id
Name
IsActive

ProductSpecification Table:
ProductId
SpecificationId

Specifications Table:
Id
Name
IsActive

ProductSpecifications is an association table. I also have the following defined in my context class:

public DbSet<Product> Products { get; set; }

EDIT

Please see my updated original post. I changed the Id of the Products and Specifications tables.

In my context class I have the following:

public DbSet<Product> Products { get; set; }
public DbSet<Specification> Specifications { get; set; }

In my repository I have the following:

public Product GetById(int id)
{
     return db.Products
          .Include("Specifications")
          .SingleOrDefault(x => x.Id == id);
}

My Product class (partial):

public class Product : IEntity
{
     public int Id { get; set; }
     public string Name { get; set; }
     public bool IsActive { get; set; }
     public ICollection<Specification> Specifications { get; set; }
}

My Specification class:

public class Specification : IEntity
{
     public int Id { get; set; }
     public string Name { get; set; }
     public bool IsActive { get; set; }
     public ICollection<Product> Products { get; set; }
}

This was all that I did from Slauma’s answer. I did not do the mapping manually as what he said I should, but I first need to understand the following:

Given my classes and tables from above, what exactly does the EF 4.1 naming conventions state on how it handles association tables? The reason why I ask is because I get the following error in my GetById method:

Invalid object name 'dbo.SpecificationProducts'.

EDIT 2

I forgot to mention the following 🙂 A product can have a specification height as value. And for this height I need to specify a value. Like 100 inches. So I modified the ProductSpecifications table to have a value column called SpecificationValue, this column will contain the value 100 inches. How would I modify the code to retrieve this value as well? I need to display it on my view.

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

    In a many-to-many relationship you only define classes for the entities you want to associate but not an entity for the association table. This table is “hidden” in your model and managed by Entity Framework automatically. So you can define these classes:

    public class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
    
        public ICollection<Specification> Specifications { get; set; }
    }
    
    public class Specification
    {
        public int SpecificationId { get; set; }
        public string Name { get; set; }
    
        public ICollection<Product> Products { get; set; }
    }
    

    This is usually enough to define a many-to-many relationship. EF would create a join table from this mapping. If you already have such a table in the database and its naming doesn’t follow exactly the naming conventions of Entity Framework you can define the mapping manually in Fluent API:

    modelBuilder.Entity<Product>()
        .HasMany(p => p.Specifications)
        .WithMany(s => s.Products)
        .Map(c =>
            {
                c.MapLeftKey("ProductId");
                c.MapRightKey("SpecificationId");
                c.ToTable("ProductSpecification");
            });
    

    Edit

    You can then load the specifications of a product by using Include for example:

    var productWithSpecifications = context.Products
        .Include(p => p.Specifications)
        .SingleOrDefault(p => p.ProductId == givenProductId);
    

    This would load the product together with the specifications. If you only want the specifications for a given product id you can use the following:

    var specificationsOfProduct = context.Products
        .Where(p => p.ProductId == givenProductId)
        .Select(p => p.Specifications)
        .SingleOrDefault();
    

    …which returns a collection of specifications.

    Edit 2

    The naming conventions of EF Code-First will assume a name of the join table built as combination of the two related class names and then pluralize it. So, without the explicite mapping to your table name ProductSpecification EF would assume ProductSpecifications (Plural) and build queries with that name as table name. Because this table doesn’t exist in the database you get the exception “Invalid object name ‘dbo.SpecificationProducts’.” when you run a query. So, you must either rename the table in the database or use the mapping code above.

    Edit 3

    I would strongly recommend to use the explicite mapping in any case because the join table name EF assumes depends on the order of the DbSets in your context. By changing the order of those sets the join table could be SpecificationProducts. Without the explicite mapping to a fixed table name a (usually unimportant) swapping of the sets in the context could break your working application.

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

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string

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.