locked
Self-Referencing relationship fails in EF RRS feed

  • Question

  • I have a base class from which almost everything else inherits..

    public abstract class MyBaseClass

    {

    public int Id { get; set; }    public int OwnerId{get; set; }    public virtual Org Owner{ get; set; } } public abstract class Party: MyBaseClass {

    } public class Org: Party {    public string Name { get; set; }    public DateTime CreationDate { get; set; } }


    My data context has a line like this:

    public DbSet<Org> Orgs { get; set; }

    When executing a query like this...

    var orgs = db.Orgs;

    I get this error:

    Multiplicity is not valid in Role 'Org_Owner_Source' in relationship 'Org_Owner'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

    In order to overcome this problem, I added the following Fluent API code:

       
    modelBuilder.Entity<Org>().HasRequired(e => e.Owner).WithMany().HasForeignKey(e => e.OwnerId).WillCascadeOnDelete(false);

    This resolves the Invalid Multiplicity error but when I subsequently run my query, it returns a "Sequence contains no elements error".

    Running a migration also throws the 'Sequence contains no elements' error when querying anything that touches Orgs..

    I can overcome this error by modifying the Fluid API to this:

    modelBuilder.Entity<MyBaseClass>().HasRequired(e => e.Owner);

    But then I get an Invalid Column Name 'Owner_Id" error, which is obviously being created dynamically at runtime by EF based upon convention since there is no such column anywhere in the code or database.

    Another way I have been trying it is with the Fluid Mapping removed and the following DataAnnotation added to the class definitions:

    public abstract class MyBaseClass

    {

       public int Id { get; set; }

       [ForeignKey("Owner")]

       public int OwnerId{ get; set; }

       public virtual Org Owner{ get; set; }

    }

    public abstract class Party: MyBaseClass

    {

    }

    public class Org: Party

    {

       public string Name { get; set; }

       public DateTime CreationDate { get; set; }

    }

    This solution throws the "Multiplicity is not valid in Role 'Org_Owner_Source' in relationship 'Org_Owner'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'." error.

    Any ideas would be greatly appreciated since it is Day #3 on this problem with no solution in sight.

    Tuesday, May 14, 2013 5:16 PM

All replies

  • Hi Spade,

    Unfortunately if I copy all the entity classes and DbContext to my project, I didn't get the same exception.

    Could you please upload a demo project to SkyDrive if possible? We can have a look what is the problem.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 16, 2013 5:54 AM