locked
EF CF Many to Many and One to Many together RRS feed

  • Question

  • 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'


    Monday, June 23, 2014 10:27 AM

Answers

  • Hello,

    I am confused since you are trying to build the many to many relationship, I don’t see that there is a collection type in your Stage entity class. Here, with your model, I change it a little and it could create a many to many and one to many relationships together as:

    public class Sample20140624Context : DbContext
    
        {
    
            public DbSet<Process> Processes { get; set; }
    
            public DbSet<Stage> Stages { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Process>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
    
                modelBuilder.Entity<Stage>().Property(s => s.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
    
                modelBuilder.Entity<Process>().HasMany(s => s.Stages).WithMany(s => s.Processes);
    
    
                modelBuilder.Entity<Process>().HasRequired(s => s.CurrentStage).WithMany(s => s.Processes).HasForeignKey(p => p.CurrentStageID).WillCascadeOnDelete(false);
    
            }
    
        }
    
    
        public class Process
    
        {
    
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public int CurrentStageID { get; set; }
    
            public virtual Stage CurrentStage { get; set; }
    
            public virtual ICollection<Stage> Stages { get; set; }
    
        }
    
    
        public class Stage
    
        {
    
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public virtual ICollection<Process> Processes { get; set; }
    
            public virtual ICollection<Process> ProcessForMany { get; set; }
    
        }
    

    The relationship is shown as:

    You can have a try and if I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Wednesday, July 2, 2014 3:34 AM
    Tuesday, June 24, 2014 7:23 AM