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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:17:08+00:00 2026-05-18T03:17:08+00:00

I am trying to define a one to many relationship between Category and Project

  • 0

I am trying to define a one to many relationship between Category and Project (a Category can have one or many Projects, a Project can have one or no Category)

public class Project : Entity {

    public virtual string Title { get; set; }

    public virtual Guid? CategoryId { get; set; }
    public virtual Category Category { get; set; }
}

public class Category : Entity {       
    public virtual string Name { get; set; }
    public virtual ICollection<Project> Projects { get; set; }
}

I have defined the following mappings:

            modelBuilder.Entity<Project>()
            .MapSingleType(p => new {
                ProjectId = p.Id,
                p.CategoryId,
                p.Title,
                p.Slug,
                p.ShortDescription,
                p.Description,
                p.CreatedOn,
                p.UpdatedOn
            })
            .ToTable("Projects");

        modelBuilder.Entity<Category>()
            .MapSingleType(c => new {
                CategoryId = c.Id,
                c.Name,
                c.CreatedOn,
                c.UpdatedOn
            })
            .ToTable("Categories");

        // relationships
        modelBuilder.Entity<Project>()
            .HasOptional<Category>(p => p.Category)
            .WithMany()
            .HasConstraint((p, c) => p.CategoryId == c.Id);

Now although this appears to be working fine, EF is still generating a Categories_Products table (used for many to many associations).

I’ve disabled the default database initializer yet this table is still being generated. What am I doing wrong?

Thanks
Ben

  • 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-18T03:17:09+00:00Added an answer on May 18, 2026 at 3:17 am

    I removed the project and category mapping code and let EF use default conventions to create the database. This created the relationship I expected (one to many between category and project).

    I would add that the only reason that I was explicitly defining the mapping was because EF does not appear to handle base classes very well. I had a base class “Entity” with a single property “Id” that all my entities inherited from. This caused so many problems with CTP4 that I just swapped it with an interface IEntity. This still gave me the constraints I needed when working with generic repository classes.

    Hopefully entity base classes will be better supported in the RTM

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

Sidebar

Related Questions

I'm trying to define a many-to-one field in the class that is the Many.
i'm trying to remove an item from a one to many list and have
I'm trying to generate a CSS class that will help me define a page
I have small problem when trying to create many-to-many link in doctrine. I have
I'm getting a NoMethodError when trying to access a method defined in one of
Im trying to define a dataTemplate for a business object in my wpf application
I'm trying to define a task that emits (using echo) a message when a
I'm trying to define a table to store student grades for a online report
I am trying to define a WCF contract that returns an interface, something like
I'm trying to dynamically define functions that call through to another function that takes

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.