I'm trying to design a Category table as seems below :
```C#
public class Category
{
public int Id { get; set;}
public string Name { get; set; }
public int Order { get; set; }
public bool IsActive { get; set; }
//ForeignKeys
[ForeignKey("ParentCategory")]
public int? ParentCategoryId { get; set; }
[ForeignKey("CategoryAttribute")]
public int CategoryAttributeId { get; set; }
//Navigations
public ICollection<Categorization> Categorizations { get; set; }
public ICollection<Category> ChildrenCategories { get; set; }
public Category ParentCategory { get; set; }
public CategoryAttribute CategoryAttribute { get; set; }
}
```
And I got this error :
System.Data.SqlClient.SqlException: 'Introducing FOREIGN KEY constraint 'FK_Categories_Categories_CategoryId' on table 'Categories' 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 or index. See previous errors.'
And this :
C:\Users\zeker\source\repos\CodeCamp-Auth-1\CodeCamp.Web>dotnet ef migrations add 2 --project ../CodeCamp.Data
fail: Microsoft.EntityFrameworkCore.Database.Command[200102]
Failed executing DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Categories] (
[Id] int NOT NULL IDENTITY,
[CategoryAttributeId] int NOT NULL,
[CategoryId] int NOT NULL,
[IsActive] bit NOT NULL,
[Name] nvarchar(max) NULL,
[Order] int NOT NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Categories_CategoryAttributes_CategoryAttributeId] FOREIGN KEY ([CategoryAttributeId]) REFERENCES [CategoryAttributes] ([CategoryId]) ON DELETE CASCADE,
CONSTRAINT [FK_Categories_Categories_CategoryId] FOREIGN KEY ([CategoryId]) REFERENCES [Categories] ([Id]) ON DELETE CASCADE
);
System.Data.SqlClient.SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_Categories_Categories_CategoryId' on table 'Categories' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
I'm trying to Parent-Children Category self-referencing relation with code first. I've tried many solution in forums(usually by using Fluent API and many of which aren't supported with EntityFrameworkCore 2.1.1) but could find no solution. Maybe i need to change deletion method to "ON DELETE NO ACTION" but i found no way.
EF Core version: (2.1.1)
Database Provider: ( Microsoft.EntityFrameworkCore.SqlServer)
IDE: (e.g. Visual Studio 2017 15.4)
@zekeriyakoca I have not been able to reproduce this behavior with the code posted. This will result in a Parent-Children self-referencing relationship:
```C#
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public int Order { get; set; }
public bool IsActive { get; set; }
//ForeignKeys
[ForeignKey("ParentCategory")]
public int? ParentCategoryId { get; set; }
//Navigations
public ICollection<Category> ChildrenCategories { get; set; }
public Category ParentCategory { get; set; }
}
Adding back in the other types still results in the self-referencing relationship, plus the other two:
```C#
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public int Order { get; set; }
public bool IsActive { get; set; }
//ForeignKeys
[ForeignKey("ParentCategory")]
public int? ParentCategoryId { get; set; }
[ForeignKey("CategoryAttribute")]
public int CategoryAttributeId { get; set; }
//Navigations
public ICollection<Categorization> Categorizations { get; set; }
public ICollection<Category> ChildrenCategories { get; set; }
public Category ParentCategory { get; set; }
public CategoryAttribute CategoryAttribute { get; set; }
}
public class CategoryAttribute
{
public int Id { get; set; }
}
public class Categorization
{
public int Id { get; set; }
}
Can you please post a runnable project/solution or complete code listing that demonstrates the behavior you are seeing?
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.
Same issue on my Notebook, but i dont have it on my PC, using same code. How can it be?
Most helpful comment
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.