0

This is scenario one, which works fine:

public class Domain
{
    public int DomainId { get; set; }

    [InverseProperty("Domain")]
    public virtual ICollection<Person> Persons { get; set; }

    [InverseProperty("Domain")]
    public virtual ICollection<Group> Groups { get; set; }
}

public class Person
{
    public int PersonId { get; set; }
    public virtual Domain Domain { get; set; }
    public virtual ICollection<Group> Groups { get; set; }
}

public class Group
{
    public int GroupId { get; set; }
    public virtual Domain Domain { get; set; }
    public virtual ICollection<Person> Members { get; set; }
}

And this is scenario two, which fails.

public class Domain
{
    // Same as scenario 1...
}

public class Person
{
    public int PersonId { get; set; }
    public int DomainId { get; set; }             // <--- new
    [ForeignKey("DomainId")]                      // <--- new
    public virtual Domain Domain { get; set; }
    public virtual ICollection<Group> Groups { get; set; }
}

public class Group
{
    public int GroupId { get; set; }
    public int DomainId { get; set; }              // <--- new
    [ForeignKey("DomainId")]                       // <--- new
    public virtual Domain Domain { get; set; }
    public virtual ICollection<Person> Members { get; set; }
}

The error message in scenario 2 is the following: The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = FK_dbo.GroupMembers_dbo.Persons_MemberId ]

Both scenarios have this mapping (many to many), inside OnModelCreating's method.

        modelBuilder.Entity<Group>()
            .HasMany(group => group.Members)
            .WithMany(member => member.Groups)
            .Map(m =>
            {
                m.ToTable("GroupMembers");
                m.MapLeftKey("GroupId");
                m.MapRightKey("MemberId");
            });

What am I doing wrong?! What I want to achieve is perfectly reasonable

tshepang
  • 12,111
  • 21
  • 91
  • 136
sports
  • 7,851
  • 14
  • 72
  • 129

1 Answers1

1

Either you're using a different version of EF to me (I'm using 5), or you're not including some code which is causing your problem. I created a context containing the code you've provided, and the only error I got was an error about multiple cascade paths (Domains being deleted via Persons or via Groups). I removed Cascade delete on one of the relationships, and it works fine.

Here's my entire context class which works with no errors:

public class TestContext : DbContext
{
    public DbSet<Person> People { get; set; }

    public DbSet<Group> Groups { get; set; }

    public DbSet<Domain> Domains { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Group>()
           .HasMany(group => group.Members)
           .WithMany(member => member.Groups)
           .Map(m =>
           {
               m.ToTable("GroupMembers");
               m.MapLeftKey("GroupId");
               m.MapRightKey("MemberId");
           });

        modelBuilder.Entity<Domain>()
            .HasMany(d => d.Groups).WithRequired(g => g.Domain)
            .WillCascadeOnDelete(false);
    }
}
Richard
  • 29,854
  • 11
  • 77
  • 120
  • Damn I cant figure out what is happening – sports Apr 15 '13 at 00:13
  • Is it possible for you to try on EF 4.4 ? It seems that I cant upgrade to 5 with Visual Studio 2010 – sports Apr 18 '13 at 23:04
  • I don't have VS2010 anymore. VS version shouldn't matter - have you tried `Update-Package EntityFramework` from the nuget package manager console? – Richard Apr 19 '13 at 08:15
  • Yes, I tried. It does matter because VS sets a restriction on what NET version one is using (4 or 4.5) and that puts a restriction on what EF i'm using. (Indeed i'm referencing EF 5 dll but it uses 4.4 at the end) – sports Apr 19 '13 at 12:39
  • Also works fine in EF4.4 (Framework v4 with EntityFramework 5 installed, which has version number 4.4). Using a default blank project, which will be using SQL Express. If you're using SQLCE it might be different behaviour. – Richard Apr 19 '13 at 15:06