I've done research about that, but I didn't find any solution. I'm using SQL CE 4.0 and I want to make relations like:
public class Process
{
[Key]
public int Id {get;set;}
public string Name {get; set;}
public int CurrentStageID {get;set;}
[ForeignKey("CurrentStageID")]
public virtual Stage CurrentStage {get;set;}
public virtual ICollection<Stage> Stages {get;set;}
}
public class Stage
{
[Key]
public int Id {get;set;}
public string Name {get; set;}
public int ProcessId {get;set;}
[ForeignKey("ProcessId")]
public virtual Process Process {get;set;}
}
But during database initialization I get Exception:
The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = FK_dbo.Stages_dbo.Processes_ProcessId ]
Such relation is correct?
I also use OnModelCreating()
method:
modelBuilder.Entity<Process>()
.HasMany(p=>p.Stages)
.WithRequired(s=>s.Process)
.HasForeignKey(s=>s.ProcessId);
Anyone can help me with that?
AD. If I add .WillCascadeOnDelete(false);
I
get:
One or more validation errors were detected during model generation:
SQLCEEngine.Proces_Etapy: : Multiplicity conflicts with the referential constraint in Role 'Process_Stages_Source' in relationship 'Process_Stages'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must
be '1'