A relationship is in the Deleted state

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 01:43:05

问题


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

An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.

The inner exception is:

A relationship from the 'User_Availability' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'User_Availability_Target' must also in the 'Deleted' state.

User looks like this:

....
ICollection<Availability> Availability { get; set; }

Availability looks like this:

int ID { get; set; }
User User { get; set; }
DateTime Start { get; set;
DateTime End { get; set; }

Configuration is as follows:

HasMany(x => x.Availability).WithRequired(x => x.User);
HasRequired(x => x.User).WithMany(x => x.Availability);

The code causing the problem is:

user.Availability.Clear();

I've looked at other alternatives such as using the DbSet to remove items, but I don't feel my code will be as clean. Is there a way to accomplish this by clearing the collection?


回答1:


The only way that I'm aware of to make it work is defining the relationship as an identifying relationship. It would required to introduce the foreign key from Availability to User as a foreign key into your model...

public int ID { get; set; }
public int UserID { get; set; }
public User User { get; set; }

...and make it part of the primary key:

modelBuilder.Entity<Availability>()
    .HasKey(a => new { a.ID, a.UserID });

You can extend your mapping to include this foreign key (just to be explicit, it isn't required because EF will recognize it by convention):

modelBuilder.Entity<Availability>()
    .HasRequired(a => a.User)
    .WithMany(u => u.Availability)
    .HasForeignKey(a => a.UserID);

(BTW: You need to configure the relationship only from one side. It is not required to have both these mappings in your question.)

Now you can clear the collection with user.Availability.Clear(); and the Availability entities will be deleted from the database.




回答2:


There is one trick. You can delete entities without using special DbSet:

(this.dataContext as IObjectContextAdapter).ObjectContext.DeleteObject(entity);

Execute this for each item in Availability collection before clearing it. You don't need 'identifying relationships' for this way.




回答3:


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).



来源:https://stackoverflow.com/questions/17850702/a-relationship-is-in-the-deleted-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!