I reverence an entity "monitoring" in two ways:
- As part of a list of monitorings in a company
- As active (= currently selected) monitoring of a company
public class Company
{
[Key]
[DataMember]
public virtual Guid CompanyId { get; set; }
[DataMember]
public virtual Guid? MonitoringId { get; set; }
[DataMember]
[ForeignKey("MonitoringId")]
public virtual Monitoring ActiveMonitoring { get; set; }
[DataMember]
public virtual ICollection<Monitoring> Monitorings { get; set; }
}
public class Monitoring
{
[Key]
[DataMember]
public virtual Guid MonitoringId { get; set; }
[DataMember]
public virtual Guid CompanyId { get; set; }
[DataMember]
[ForeignKey("CompanyId")]
public virtual Company Company { get; set; }
}
If I create a new Company and add a new Monitoring to the list of monitorings, I am able to save it with breeze. After saving the new Company, I am able to set the alredy added Monitoring as ActiveMonitoring and save the Company again.
However, If I try to add the Monitoring and set it as ActiveMonitioring and save the Company only once, I get following error:
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
Here is example JavaScript code for creating the new Company:
var company = unitofwork.companyRepository.create();
var monitoring = unitofwork.monitoringRepository.create();
//add monitoring to the list of monitorings
monitoring.company(company);
company.monitorings.push(monitoring);
//set monitoring as active monitoring
company.activeMonitoring(monitoring);
unitofwork.commit();
I use EntityFramework 6 and Breeze 1.5.4
I tried to use the annotation "InverseProperty" to make the relations more explicit but did not succeed. I also found some related stackoverflow questions but I could not find a solution for my specific issue:
Unable to determine a valid ordering for dependent operations
How to specify two navigation properties from entity X to the same target entity, Y?
My classes allow following case: a Monitoring could be part of a list of one Company and be set as active Monitoring for another Company. However, I do not want to allow that case: a Monitoring should only be allowed to be the active Monitoring if it is also part of the list of monitorings of the same Company. Is this the underlying cause of my issue?
Does the Monitoring need two references to the Company, e.g. CompanyForListReference and CompanyForActiveReference? Or is there a solution that works with only one CompanyId?
I also thought about not saving an instance as active Monitoring, but only the index of the active Monitoring in the list of monitorings, e.g. "IndexOfActiveMonitoring". However, if I would like to access the active Monitoring, I would need some extra code to look up the active Monitoring.
=> What is the recommended way to implement the two references from Company to Monitoring?