There’s a many-to-many UserFeed table that stands between User and Feed and denotes a twitter-like follow relationship.
It only has two fields, which form a composite key: UserID and FeedID.
I need to write a method that will subscribe or unsubscribe a user from a feed based on a boolean flag.
public void SetSubscriptionFlag (int userId, int storeId, bool subscribe)
{
}
I’m new to Entity Framework so I’m trying to find and follow an “EF-ish” way to accomplish this.
My initial thoughts are:
- Instead of working with the middle
UserFeedclass, I should create a many-to-manySubscriptionsproperty (EDIT: hit limitations here); - After I’ve done so, I’ll need to fetch a
Userinstance by ID, check whether it has givenFeedin itsSubscriptionsand add/delete it depending on the flag and current existence; - Figure out how to avoid racing conflicts when there is a time interval before the check and adding/deleting and user manages to submit two adding or deletion requests;
- Optimize my code as to avoid unneccessary SELECTs, if any occur, because all I really want to do is a single SELECT and single INSERT/DELETE.
A relevant code snippet and comment on my points is highly appreciated.
Thanks!
You can use dummy objects – it definitely works for insert and I hope it can be used for delete as well:
Create new relation:
Remove existing relation: