0

These are my models:

public class Application
{        
    public Guid ID { get; set; }         
    public virtual Project MainProject { get; set; }
    public virtual ICollection<Solution> Solutions { get; set; }
}

public class Solution
{
    public Guid Id { get; set; }        

    public virtual Application Application { get; set; }
    public virtual ICollection<Project> Projects { get; set; }

}

public class Project
{
    public Guid Id { get; set; }

    public virtual ICollection<Solution> Solutions { get; set; }

}

So basically, an Application has a list of Solutions, and a Solution has a list of Projects. An Application also has a Main Project (which will be somewhere in the group of Projects that is accessible through the Application's Solutions, but that's not forced by the DB).

I've got an issue when I try to add a new Application that has the MainProject property set.

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

I don't understand what the problem is. MainProject is not mandatory, in fact the insert works if I don't set that property. There is a circular dependency, but there is a clear order that works

  1. Insert application with no main project
  2. Insert solutions
  3. Insert projects
  4. Update application to set relevant project as main project.

Is there any way to tell Entity Framework to do this?

edit:

Here are the configurations:

public ApplicationConfiguration()
        : base()
    {
        HasKey(a => a.ID);
        ToTable("Applications");            
        HasOptional(a => a.MainProject);
    }

public SolutionConfiguration()
        : base()
    {
        ToTable("Solutions");
    }

public ProjectConfiguration()
        : base()
    {
        ToTable("Projects");
    }
MrZarq
  • 258
  • 3
  • 12

1 Answers1

0

You can configure this in the DbModelBuilder using the fluent api or in the Entity Models using data annotations. Basically, you have to tell EF which Entity is the principal end and which is the dependent (think cascade delete...).

For example, if Application is the principal and there can be 0 or 1 MainProject, and MainProject shall not exist without Application, and MainProject shall be deleted when Application gets deleted:

ModelBuilder.Entity<Application>().HasOptional(a => a.MainProject).WithRequired().WillCascadeOnDelete(true);

Application has many Solutions, and Application is required for a valid Solution, with backlink:

ModelBuilder.Entity<Application>().HasMany(a => a.Solutions).WithRequired(s => s.Application);

Solution has many Projects, with backlink:

ModelBuilder.Entity<Solution>().HasMany(s => s.Projects).WithMany(p => p.Solutions);

See Configuring Relationships with the Fluent API

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
  • Application isn't accessible from Project without going through Solution... – MrZarq Aug 16 '16 at 14:30
  • Right, then just do not define an expression for the backlink in the `WithRequired()` part. I also added a link to MSDN that shows the different options for different types of relationships. – Georg Patscheider Aug 16 '16 at 14:32
  • Didn't help. Just adding HasOptional still gives the exception. – MrZarq Aug 17 '16 at 07:56
  • Did you configure all relationships or just the one between Application and MainProject? – Georg Patscheider Aug 17 '16 at 08:06
  • I just looked through our code to find a similar config. Seems like we also had the "Unable to determine a valid ordering" error, despite the correct ModelBuilder config. The workaround is as you described above, add the entities in the correct order (store Application, Solutions and Projects first, then add link to MainProject and store again). See also http://stackoverflow.com/questions/10584341/entity-framework-5-0b2-code-first-one-to-many-and-one-to-one-for-the-same-table – Georg Patscheider Aug 17 '16 at 08:59
  • So no workaround to avoid having more than 1 SaveChanges? Oh well, thanks for the effort! – MrZarq Aug 17 '16 at 09:01
  • Seems to be a longstanding bug in EF that MS just wont fix :( https://connect.microsoft.com/VisualStudio/feedback/details/633296/support-for-creation-of-entities-with-circular-references-in-entity-framework#details – Georg Patscheider Aug 17 '16 at 09:04