The collection argument 'foreignKeyPropertyNames' must contain at least one element

0

I have two models with certain properties:

public class Issue {
    public int Id { get; set; } 
}

public class Action {
    public int Id { get; set; } 
}

This was the last working code. Then as I forgot to add Issue as a foreign key property to Action I did it and it became:

public class Action {
    public int Id { get; set; } 
    public Issue Issue { get; set; }
    public int IssueId { get; set; }
}

The solution built, migration up method created and ef update database failed with the following error:

Applying migration '20180220102738_Action.issue'. Failed executing DbCommand (20ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] ALTER TABLE [Actions] ADD CONSTRAINT [FK_Actions_Issues_IssueId] FOREIGN KEY ([IssueId]) REFERENCES [Issues] ([Id]) ON DELETE CASCADE; System.Data.SqlClient.SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_Actions_Issues_IssueId' on table 'Actions' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Then I tried to change the onDelete: ReferentialAction.Cascade to onDelete: ReferentialAction.SetNull or anything else but nothing worked and btw I don't understand what could be its problem, this would be the third foreign key on this table.

My next step was to remove last migration but it started to complain about the following:

System.ArgumentException: The collection argument 'foreignKeyPropertyNames' must contain at least one element. at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty[T](IReadOnlyList1 value, String parameterName) at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceCollectionBuilder2.HasForeignKey(String[] foreignKeyPropertyNames) at ISSupport.Models.ISSupportContext.<>c.b__69_7(EntityTypeBuilder`1 b) in

I could roll back to the last commit but I don't see what did I do and what should I avoid in the next round. Google didn't help with this error. .NET Core 2.0 EF Core 2.0 Sql Server 13.0.1742

Update I had to do a rollback and added the foreign key again. It complained again about multiple cascade paths and now I changed it in the migration Up() method to onDelete: ReferentialAction.NoAction and run update successfully. I don't know what I had f*#!d up before and still need an explanation why do I have to set ReferentialAction.NoAction

sql-server
asp.net-core
.net-core
entity-framework-core
ef-core-2.0
asked on Stack Overflow Feb 20, 2018 by Perrier • edited Feb 20, 2018 by Perrier

2 Answers

0

This is what you had to do:

public class Issue
{
    public int Id { get; set; }
}

public class Action
{
    public int Id { get; set; }

    [ForeignKey("IssueId")]
    public Issue Issue { get; set; }
    public int IssueId { get; set; }
}

You have to define what is going to be your foreign key property.

answered on Stack Overflow Feb 20, 2018 by pitaridis
0

In migration Up() method I had to switch the Foreign key ReferentialAction to onDelete: ReferentialAction.NoAction

answered on Stack Overflow Feb 23, 2018 by Perrier

User contributions licensed under CC BY-SA 3.0