Cascading Object Deletion

First of all, I apologize if I missed some basic things here, but I'm new to EF and still trying to create DB code first ....

I have a similar problem with this. Representing a FOREIGN KEY constraint may cause loops or several cascading paths , but it doesn't seem to come from the comments there, what I need to do with my particular model. When I try to update the database after adding in public virtual Actor actor { get; set; }to my UseCase class, I get this error:

Introducing FOREIGN KEY constraint 'FK_dbo.UseCase_dbo.Actor_ActorID' on table 'UseCase' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.

I know this should be related to how my FK restrictions are configured (maybe something has to do with deleting the use case, which means that I will delete data from several other tables).

I tried disabling cascading deletion, but still getting an error message:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //prevent table names created by entity framework being pluralised
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            //Turn off delete cascades between parent child tables. May need to put this back in future, but for the time being it is stopping the database being updated through the package manager console (error is that a foregin key constraint may cause cycles or multiple cascade paths) 
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
        }

. , , , . UseCase, UseCases. - , ?

, Project > Actors > Use Cases. , public virtual int ProjectID { get; set; } public virtual Project project { get; set; } UseCase?

!

 public class Project
    {
        public virtual int ID {get; set;}
        [DisplayName ("Project Name")]
        public virtual string ProjectName { get; set; }
        [DisplayName("Client")]    
        public virtual string ClientID { get; set; }
        public virtual string Description { get; set; }
        [DisplayName("Use Cases")]
        public virtual ICollection <UseCase> UseCases { get; set; } 

   }

public class UseCase

{
    public virtual int ID { get; set; }
    [Required]
    public virtual int ProjectID { get; set; }
    public virtual int ActorID { get; set; }
    [Required]
    public virtual Actor actor { get; set; }
    public virtual string Title { get; set; }
    public virtual Level? Level { get; set; }
    public virtual string Precondition { get; set; }
    public virtual string MinimalGuarantee { get; set; }
    public virtual string SuccessGuarantee { get; set; }
    public virtual ICollection<Step> Steps { get; set; }
    public virtual ICollection<Extension> Extensions { get; set; }
    public virtual ICollection<Query> Queries { get; set; }

}

public class Actor
{
    public virtual int ID { get; set; }
    public virtual int projectID { get; set; }
    public virtual Project project { get; set; } 
    public virtual string Title { get; set; }

    [DataType(DataType.MultilineText)]
    public virtual string Description { get; set; }

}

, , . , , , Update-Database. .

, , , . , , , . , . , - :-(

    modelBuilder.Entity<Actor>()
    .HasMany(a => a.useCases)
    .WithRequired(uc => uc.actor)
    .HasForeignKey(uc => uc.ActorID)
    .WillCascadeOnDelete(true); // and this works

    modelBuilder.Entity<Project>()
    .HasMany(p => p.actors)
    .WithRequired(a => a.project)
    .HasForeignKey(a => a.projectID)
    .WillCascadeOnDelete(true); // this works

    modelBuilder.Entity<Project>()
    .HasMany(p => p.UseCases)
    .WithRequired(uc => uc.project)
    .HasForeignKey(uc => uc.ProjectID)
    .WillCascadeOnDelete(false); // disable this cascading delete
+1
2

, . :

Project -> UseCase
Project -> Actor -> UseCase

UseCase - Project Actor. , ProjectUseCase, Actor:

modelBuilder.Entity<Project>()
    .HasMany( p => p.UseCases )
    .WithRequired( uc => uc.Project )
    .HasForeignKey( uc => uc.ProjectID )
    .WillCascadeOnDelete( false ); // disable this cascading delete

modelBuilder.Entity<Project>()
    .HasMany( p => p.Actors )
    .WithRequired( a => a.Project )
    .HasForeignKey( a => a.ProjectID )
    .WillCascadeOnDelete( true ); // this works

modelBuilder.Entity<Actor>()
    .HasMany( a => a.UseCases )
    .WithRequired( uc => uc.Actor )
    .HasForeignKey( uc => uc.ActorID )
    .WillCascadeOnDelete( true ); // and this works

:

- Actor, UseCase FK Project ProjectID, , Actor, UseCase ProjectID - a Actor " 1", a UseCase " 2". ProjectID Actor PK, UseCaseActor FK, , Actor, a UseCase, Project, 2NF.

"" , , ProjectActorsUseCases, , Actors, Project UseCase s

+2

ActorID UseCase nullable int. EF , 2 , . - -, , , SQL Server.

Actor UseCase , Actor , UseCase, , , .

0

All Articles