0

I have a following problem. I have two entities for which I want to create many to many relationship.

public class Subsystem
{
    public int SubsystemId { get; set; }
    public string SubsystemName { get; set; }
    public virtual ICollection<Component> Components { get; set; }
}
public class Component
{
    public int ComponentId { get; set; }
    public string ComponentName { get; set; }
    public virtual ICollection<Subsystem> Subsystems { get; set; }
}

None of them is more important than the other - they can exist separately, but sometimes they can be connected via an appropriate entry in the join table. Now, after I create a migration, I get something like this:

public override void Up()
{
    CreateTable(
            "dbo.Component",
            c => new
                {
                    ComponentId = c.Int(nullable: false, identity: true),
                    ComponentName = c.String(nullable: false, maxLength: 100)
                })
            .PrimaryKey(t => t.ComponentId)

        CreateTable(
            "dbo.Subsystem",
            c => new
                {
                    SubsystemId = c.Int(nullable: false, identity: true),
                    SubsystemName = c.String(nullable: false, maxLength: 100),
                 })
            .PrimaryKey(t => t.SubsystemId);

        CreateTable(
            "dbo.SubsystemComponents",
            c => new
                {
                    Subsystem_SubsystemId = c.Int(nullable: false),
                    Component_ComponentId = c.Int(nullable: false),
                })
            .PrimaryKey(t => new { t.Subsystem_SubsystemId, t.Component_ComponentId })
            .ForeignKey("dbo.Subsystem", t => t.Subsystem_SubsystemId, cascadeDelete: true)
            .ForeignKey("dbo.Component", t => t.Component_ComponentId, cascadeDelete: true)
            .Index(t => t.Subsystem_SubsystemId)
            .Index(t => t.Component_ComponentId);

    }

I cannot set cascadeDelete to true, and that is why I keep getting the following error when I update-database:

Introducing FOREIGN KEY constraint 'FK_dbo.SubsystemComponents_dbo.Component_Component_ComponentId' on table 'SubsystemComponents' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

I am almost sure that this is the result of cascadeDelete : true.

I don't know how to force it to be set to false. I am using FluentAPI, and everything has to be configured in Configuration class for each entity.

vanpersil
  • 764
  • 1
  • 8
  • 26
  • There must be more associations than you show here. In a junction table between two different entities the two FKs can be cascading. I think both classes are also connected through association another path. – Gert Arnold Sep 22 '18 at 17:34
  • No, I don't think so - when I change manually cascadeDelete: true to false in migration class and then issue the update-database command, it works fine and the db looks and behaves exactly how I want it to behave (no cascading). So, the actual problem is how to setup configuration class in order to generate migration class with cascadeDelete: false. – vanpersil Sep 22 '18 at 17:50
  • Well, kinda weird... Anyway, consider [this](https://stackoverflow.com/q/13705441/861716) a duplicate. – Gert Arnold Sep 22 '18 at 17:51
  • I saw this post, but... I think that it was asked a long time ago, and the answer is not valid now (for EF 6) - there is no WillCascadeOnDelete method available. Global disabling ManyToManyCascadeDeleteConvention is no option for, me, as well as manual editing migration file. – vanpersil Sep 22 '18 at 19:52
  • @Marcin Then you are stuck (you are right, there isn't `WillCascadeOnDelete` option even in the latest EF6.2 - the last answer from the link is incorrect, I have no idea how it can be upvoted). The only option is to switch to explicit junction entity model. – Ivan Stoev Sep 22 '18 at 20:21

0 Answers0