c# - How do you define a model for a User-to-User relationship in entity framework -
You can use EF in general
ApplicationUser user {get; Set; }
To define the property in relation to users but I am confused how to make it between two users for a property, if it makes sense? Basically I want to define a model for relationships between users. E.g.
UserA and UserB have the following relationships:
bool friends {get; Set; } Bool blocked {get; Set; }
I am also thinking that this is ideal for "blocked" property because I want both of them to be able to block each other and unblock them. Like UserA Block UserB then decides to unblock it, and therefore the blocked property is changed from right to wrong. But if UserB has blocked UserA too, then the property remains true.
Edit 28/07
Public Class ApplicationUser: IdentityUser {... Public Virtual Ilkonac & lt; UserRelationships & gt; Relationship {get; Set; }} Public class UserRelationships {public int UserRelationshipsId {get; Set; } Public UserRelationships () {this.Friends = false; this. Block = false; } Public listing & lt; ApplicationUser & gt; User {Received; Set; } Public Butter Friends {Received; Set; } Public bool blocked {get; Set; } Public string blocker {get; Set; } Public string secondblocker {received; Set; }}
You can make many-to-many (pure) in the EF but it only tracks left and right ID, and therefore, you must do so in another table if you want to audit the time of friendship or block.
Instead, I would encourage you to use the following syntax:
public class ApplicationUserRelationship {public virtual application User LeftUser {get; Set; } Public virtual applicationUser RightUser {get; Set; } Public Virtual Relationship Relationship Type {get; Set; } Public Virtual Date Time? Created DateTime {Received; Set; } Public Virtual Date Time? Updated {DATA; Set; } ...
You can set the relationship
with string
or bool
as If you understand this ( IsFriend
, IsBlocked } or enum
it is understandable.
Your ApplicationUser
is now with you:
Public Virtual Icons & lt; ApplicationUserRelationship & gt; ApplicationUserRelationships {get; Set; }
Then in a fluent way:
modelbuilder.Entity & lt; ApplicationUser & gt; () .HasMany (u = & u; u.ApplicationUserRelationships). Compulsory with (u = & gt; u.LeftUser); Modlbilder Anti & lt; ApplicationUser & gt; () .HasMany (u = & u; u.ApplicationUserRelationships). With compulsory (u = & gt; u right user);
Comments
Post a Comment