Milleu Milleu - 3 months ago 43
C# Question

Entity Framework one-to-many and many-to-many relationship

im currently learning the entity framework and been miserably failing with this task for a couple of days... I would like to achieve the following Database Scheme:

TableA:
ClassA_ID
ClassB_ID(foreign key - one to one)

TableATableB:(one-to-many relationship)
ClassA_ID
ClassB_ID

TableB:
ClassA_ID(foreign key - one to one)
ClassB_ID


So I Actually want a specific one to one relationship from Class A to Class B and a one to many Relationship from ClassA to ClassB.

My C# Code Looks like this:

Class A:

public static int idcounter;
[Key]
public int id { get; set; }
public virtual List<ClassB> allClassB { get; set; }
public virtual ClassB currentClassB { get; set; }
public int? currentClassBID { get; set; }

public ClassA(){
idcounter++;
id = idcounter;
allClassB = new List<ClassB>();
currentClassB = new ClassB();
currentClassBID = currentClassB.id;
MyContext.add(this);
}


Class B:

public static int idcounter;
[Key]
public int id { get; set; }
public virtual ClassA owner { get; set; }
public int? ownerID { get; set; }

public ClassB(ClassA a){
idcounter++;
id = idcounter;
owner = a;
ownerID = a.id;
}


MyContext:

public static void add(ClassA a)
{
using (MyContext context = new MyContext ())
{
context.setB.Add(a.currentClassB);
context.setA.Add(a);
context.SaveChanges();
}
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ClassA>().HasOptional(x => x.currentClassB ).WithMany().HasForeignKey(x => x.currentClassBID ).WillCascadeOnDelete(false);
modelBuilder.Entity<ClassB>().HasOptional(x => x.ClassA).WithMany(x => x.allClassB).HasForeignKey(x => x.ownerID).WillCascadeOnDelete(false) ;
}


Its pretty similar to this question
Entity Framework Code First: How can I create a One-to-Many AND a One-to-One relationship between two tables?
but I just get a Circular Exception when saving.

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


Its pretty weird that after

context.setA.Add(a);


it adds currentClassB into allClassB without me ever mention that... it Even suddenly does that when i dont

allClassB = new List<ClassB>();


What am I doing wrong?

Answer

You have to use InverseProperty as shown below.

[InverseProperty("allClassB")]
public virtual ClassB currentClassB { get; set; }
public int? currentClassBID { get; set; }

DataAnnotations - InverseProperty Attribute:

Comments