5

I have a problem with connection between 2 entities when there is 2 navigations.

to be specific, I have the following classes:

public class TableA
{
    public TableA()
    {
        ListBs = new List<TableB>();
    }

    [Key]
    public int Id { get; set; }

    public TableB MainB { get; set; }

    public virtual ICollection<TableB> ListBs { get; set; }
}

public class TableB
{
    [Key]
    public int Id { get; set; }

    public virtual TableA refA { get; set; }

    [Required]
    public string Text { get; set; }

}

The scenario of this particular classes reflects the following: TableA has a list of TableB objects and also has 1 main TableB object(that is of course in the list too). Also a TableB object may not actualy have a reference to TableA

the fetching works. but when I try to insert new items I get the following exception:

Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

Any idea where I got anything wrong?

  • 1
    look at this http://stackoverflow.com/questions/8100568/clean-way-to-deal-with-circular-references-in-ef – ilay zeidman Jan 14 '14 at 12:11
  • the problem is not a circular reference. After a lot of fiddling and trying I narrowed it to inserting a new object(with no id) to both navigation properties. – user3190346 Jan 20 '14 at 07:51
  • I use breeze and the only work around solution I could find on this issue was to save the entity twice: first add reverenced entity to the list and save. Then set it as the extra referenced item and save again. – Stefan Jan 22 '16 at 08:51

1 Answers1

0

use this Code :

public class TableA
{
    public TableA()
    {
        ListBs = new List<TableB>();
    }

    [Key]
    public int Id { get; set; }

    public int TableB_Id { get; set; }

    [InverseProperty("TableA_Mains")]
    [ForeignKey("TableB_Id")]
    public TableB MainB { get; set; }

    [InverseProperty("refA")]
    public virtual ICollection<TableB> ListBs { get; set; }
}

public class TableB
{
    [Key]
    public int Id { get; set; }

    public int TableA_Id { get; set; }

    [Foreignkey("TableA_Id")]
    [InverseProperty("ListBs")]
    public virtual TableA refA { get; set; }

    [Required]
    public string Text { get; set; }


    [InverseProperty("MainB")]
    public virtual ICollection<TableA> TableA_Mains { get; set; }

}
Iraj
  • 1,492
  • 5
  • 18
  • 42
  • What is the meaning of [InverseProperty("TableA_Main")] ? – Stefan Jan 22 '16 at 07:50
  • Should it somehow reference the constructor of TableA? Did you test your code? Is there some documentation for such kind of inverseproperty references? I could not get it working. Please also see: http://stackoverflow.com/questions/34943464/how-to-specify-two-navigation-relations-for-ef-breeze-unable-to-determine-a-va – Stefan Jan 23 '16 at 09:15
  • Ah, now I see. TableA_Mains is the name of the collection in TableB. Sorry and thank you for the other example as well. – Stefan Jan 23 '16 at 11:19