Paul Manzotti
Paul Manzotti

Reputation: 5147

Setting up 2 relationships between the same objects in Entity Framework

I have a simple object that can be a child of a parent group object. The complication is that the parent group can be one of two "types" (not C# types though), indicated by an enum property, and each child object can be in only one of each type. This will probably make more sense with the classes:

    public class Child
    {
        public string Value { get; set; }
        public int? ParentGroup1Id{ get; set; }
        public int? ParentGroup2Id { get; set; }
        public ParentGroup ParentGroup1 { get; set; }
        public ParentGroup ParentGroup2 { get; set; }
    }

    public class ParentGroup
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public GroupType GroupType { get; set; }
        public IList<Child> Children1 { get; set; }
        public IList<Child> Children2 { get; set; }
    }

I have set up an EF map for the Child:

    public class ChildMap : EntityTypeConfiguration<Child>
    {
        public ChildMap()
        {
            HasKey(t => t.Value);

            HasOptional(c => c.ParentGroup1)
             .WithMany(pg => pg.Children1)
             .HasForeignKey(c => c.ParentGroup1Id);

            HasOptional(c => c.ParentGroup2)
             .WithMany(pg => pg.Children2)
             .HasForeignKey(c => c.ParentGroup2Id);
        }
    }

On my Context, I have set both objects up in DBSets:

    public IDbSet<Child> Children { get; set; }
    public IDbSet<ParentGroup> ParentGroups { get; set; }

When I go to a page that hits the database, I get the following error:

Additional information: Schema specified is not valid. Errors: The relationship 'ServiceLayer.Contexts.Child_ParentGroup' was not loaded because the type 'ServiceLayer.Contexts.ParentGroup' is not available.

Everything is in the ServiceLayer namespace, by the way.

I would guess that it doesn't consider these to be valid relationships, but I'm not good enough with EF to know how to set this sort of relationship up. Is there a way of getting this to work, or do I have to change it completely?

Upvotes: 1

Views: 233

Answers (1)

Paul Manzotti
Paul Manzotti

Reputation: 5147

Thanks to the answer provided here, I took out the contents of the WithMany calls, and that's fixed it.

So, my ChildMap now looks like this:

    public class ChildMap : EntityTypeConfiguration<Child>
    {
            public ChildMap()
            {
            HasKey(t => t.Value);

            HasOptional(c => c.ParentGroup1)
                .WithMany()
                .HasForeignKey(c => c.ParentGroup1Id);

            HasOptional(c => c.ParentGroup2)
                .WithMany()
                .HasForeignKey(c => c.ParentGroup2Id);
        }
    }

I'm guessing that giving it a parameter tries to set the relationship up in both directions, and the Child collections on the ParentGroup were too vague.

I now have a ParentGroupMap setting up the relationship from that end (something that I had tried in my original attempt, but it didn't work with that ChildMap):

    public class ParentGroupMap : EntityTypeConfiguration<ParentGroup>
    {
        public ParentGroupMap()
        {
            HasMany(pg => pg.Children1)
                .WithOptional(c => c.ParentGroup1)
                .HasForeignKey(c => c.ParentGroup1Id);

            HasMany(pg => pg.Children2)
                .WithOptional(c => c.ParentGroup2)
                .HasForeignKey(c => c.ParentGroup12d);
        }
    }

Upvotes: 1

Related Questions