I have an entity that has a Many-to-Many self-relationship. As an example consider this entity:
public class User
{
public int ID { get; set; }
public string UserName { get; set; }
public virtual ICollection<User> Friends { get; set; }
}
Here is how I configure the mapping:
HasMany(t => t.Friends).WithMany()
.Map(m => {
m.MapLeftKey("UserID");
m.MapRightKey("FriendID");
m.ToTable("UserFriends");
});
As this relationship is now managed by the EF, I don't really have access to the UserFriends
DbSet
in my code and cannot handle the concurrent access to it.
In order for this composition to handle concurrent access(add/remove), do I need to handle the Many-to-Many relationship myself and then add a [Timestamp]
column or is there a way to tell EF to handle this concurrently itself? Like a configuration in the model builder.
Edit: I'm using EF 6 and currently if there is a concurrent operation on the entity(e.g trying to remove a friend that currently doesn't exits on the database) I get the following error message and an DbUpdateException
:
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.