A relationship is in the Deleted state

前端 未结 3 492
臣服心动
臣服心动 2020-12-28 13:46

When I am trying to clear a collection (calling .Clear) I get the following exception:

An error occurred while saving entities that do no

3条回答
  •  没有蜡笔的小新
    2020-12-28 14:25

    In case someone has the same problem using SQLite:

    Unfortunately the accepted answer does not work with SQLite because SQLite does not support auto increment for composite keys.

    You can also override the SaveChanges() Method in the Database context to delete the children:

    //// Long Version
    //var localChilds = this.SubCategories.Local.ToList();
    //var deletedChilds = localChilds.Where(w => w.Category == null).ToList();
    //foreach(var child in deletedChilds) {
    //   this.SubCategories.Remove(child);
    //}
    
    // Short in LINQ
    this.SubCategories.Local
        .Where(w => w.Category == null).ToList()
        .ForEach(fe => this.SubCategories.Remove(fe));
    #endregion
    

    See this great Blogpost as my source (Unfortunately written in german).

提交回复
热议问题