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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:14:27+00:00 2026-06-15T04:14:27+00:00

I’m having issues applying multiple relationships (or possibly foreignkey) on two POCO objects. I’ve

  • 0

I’m having issues applying multiple relationships (or possibly foreignkey) on two POCO objects. I’ve got the first relationship many-to-many working and when the database is created it creates the three tables (Projects, Users and ProjectsUsers) needed for the relationship.

Code so far:

public class Project
{
    public int ProjectId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? CompletionDate { get; set; }
    public bool Deleted { get; set; }

    public ICollection<User> Users { get; set; }
}

public class User
{
    public User()
    {
        Name = new Name();
    }

    public int UserId { get; set; }
    public string LoginId { get; set; }
    public string Password { get; set; }
    public Name Name { get; set; }

    public ICollection<Project> ManagedProjects { get; set; }
}

public class ProjectConfiguration : EntityTypeConfiguration<Project>
{
    public ProjectConfiguration()
    {
        HasMany(x => x.Users)
            .WithMany(x => x.ManagedProjects);
    }
}

public UserConfiguration()
{
    HasMany(x => x.ManagedProjects)
        .WithMany(x => x.Users);
}

Now I want to add an optional one-to-one relationship of Project.ManagingUser -> User. However, I can’t seem to figure out how to indicate this in the configuration.

Code for what I think is needed:

public class Project
{
    public int ProjectId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? CompletionDate { get; set; }
    public bool Deleted { get; set; }

    public int? ManagingUserId { get; set; }
    public User ManagingUser { get; set; }

    public ICollection<User> Users { get; set; }
}

I don’t think the User object needs to change.

This shows my last attempt on mapping the new relationship:

public ProjectConfiguration()
{
    HasMany(p => p.Users)
        .WithMany(u => u.Projects);

    this.HasOptional(p => p.ManagingUser)
        .WithOptionalDependent()
        .Map(m=>m.MapKey("ManagingUserId"))
        .WillCascadeOnDelete(false);
}

What is happening when the database is created, I now end up with only two tables (Projects and Users). And it looks like it is only trying to setup the one-to-one relationship.

Can someone tell me what I’m missing?

Richard I’ve not changed the UserConfiguration and below is the DbContext:

public class MyDbContext : DbContext
{
    public MyDbContext() : base(Properties.Settings.Default.ConnectionString)
    {
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Project> Projects { get; set; }
}
  • 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-06-15T04:14:29+00:00Added an answer on June 15, 2026 at 4:14 am

    You probably want WithMany instead of WithOptionalDependent – it’s a one:many relationship, not a one:one.

    HasOptional(p => p.ManagingUser)
       .WithMany()
       .HasForeignKey(m => m.ManagingUserId)
       .WillCascadeOnDelete(false);
    

    EDIT
    I think you’re missing the OnModelCreating override from the DbContext class:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       base.OnModelCreating(modelBuilder);
       modelBuilder.Configurations.Add(new ProjectConfiguration());
       modelBuilder.Configurations.Add(new UserConfiguration());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
i got an object with contents of html markup in it, for example: string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.