Hallo!
We want to Drag and Drop rows inside a TreeGrid and update the ParentId of the object in the database. We use Blazor Server Side with EF Core, initially load the DB tables and then use an ObservableCollection of the local DbContext.
Current behaviour:
The problem is that apparently the TreeGrid internally removes and readds the object with the new ParentId (can be seen in the DbContext ChangeTracker DebugView) which then triggers the deletion of the child objects with foreign key relationship. For our case that means drag and dropping a Category deletes all its Articles in the database.
Summary:
Category.ParentId is updated correctly.
Category.Articles are deleted from the database.
Expected behaviour:
The ParentId of the dropped object is modified and updated in the database.
Our code looks like this:
Model:
public class Category
{ public int Id { get; set; } public string Name { get; set; } = ""; public int? ParentId { get; set; } public IList<Article> Articles { get; set; } = new List<Article>();
[ForeignKey(nameof(ParentId))]
public IList<Category> Children { get; set; } = new List<Category>();
}
public class Article
{ public int Id { get; set; } public string Name{ get; set; } = ""; public int CategoryId { get; set; }}
I have attached the razor files with the relevant code parts because there were problems copy pasting the code inside here.
Kind regards