dhamilton
dhamilton

Reputation: 173

Entity Framework 5 using multiple relationships between two POCOs

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; }
}

Upvotes: 1

Views: 1822

Answers (1)

Richard Deeming
Richard Deeming

Reputation: 31198

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());
}

Upvotes: 2

Related Questions