This Question have 2 answers right now.

How to handle self references in Entity Framework code-first?


These are my models (simplified):

public User()
        Friends = new HashSet<User>();
        Subscriptions = new HashSet<Subscription>();
        Tasks = new HashSet<Task>();
        Invitations = new HashSet<Invitation>();
        Events = new HashSet<Event>();

public Guid UserId { get; set; }
public DateTime MemberSince { get; set; }

[Index("UserNameIndex", IsUnique = true)]
public string NickName { get; set; }

public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAdress { get; set; }        
public string HashedPassword { get; set; }

public virtual ProfilePicture ProfilePicture { get; set; }

public bool Validated { get; set; } 

ICollection<Event> Events { get;  set; }
ICollection<User> Friends { get;  set; }

And the Event model:

public class Event
    public string EventName { get; set; }
    public Guid EventId { get; set; }
    public Guid UserId { get; set; } 
    public DateTime? Time { get; set; }
    public string Location { get; set; }
    public DateTime? EventDate { get; set; }        
    public virtual User User { get; set; }

    public ICollection<User> Participants { get; internal set; }        

Here's the model creation:

modelBuilder.Entity<User>().HasKey(u => u.UserId);
             HasMany<User>(u => u.Friends).

             HasMany<Event>(u => u.Events).

Now the problem is the following: my tables look like this:

It seems like the relations are not the way they should be...

User table:

enter image description here

Event table:

enter image description here

Automatically created UserEvents:

enter image description here

Now what I expected is when creating a new Event (UserId) is required there. I get a new Entry in the Events table + a new one in the UserEvents....

What am I missing here ?

By : Motivated


You have two different relations between User and Event. A one-to-many relation and a many-to-many relation.

The first is a one-to-many relationship between the event and the creator of the event (The user and userid properties on Event) When you add a new Event with the required UserId, there will not be a record created in the automatically created UserEvents table, because you have a one-to-many relationship here. So simply creating an Event with the userid will not lead to a record in the UserEvents table.

The second is the many-to-many relationship between Event and it's participants. When you add an event with participants. There would be records also inserted into the UserEvents table. Only participants will appear in the UserEvents table. You should however create your many-to-many mapping with a reference to your property Participants in the Event class to make this possible.

modelBuilder.Entity<User>().HasMany<Event>(u => u.Events).WithMany(m => m.Participants);  
By : HansVG

If you are just asking is this a valid metric then the answer is almost, it is a valid pseudometric if only .computeCost is deterministic.

For simplicity i denote f(A) := model.computeCost(A) and d(A, B) := |f(A)-f(B)|

Short proof: d is a L1 applied to an image of some function, thus is a pseudometric itself, and a metric if f is injective (in general, yours is not).

Long(er) proof:

  • d(A,B) >= 0 yes, since |f(A) - f(B)| >= 0
  • d(A,B) = d(B,A) yes, since |f(A) - f(B)| = |f(B) - f(A)|
  • d(A,B) = 0 iff A=B, no, this is why it is pseudometric, since you can have many A != B such that f(A) = f(B)
  • d(A,B) + d(B,C) <= d(A,C), yes, directly from the same inequality for absolute values.

If you are asking will it work for your problem, then the answer is it might, depends on the problem. There is no way to answer this without analysis of your problem and data. As shown above this is a valid pseudometric, thus it will measure something decently behaving from mathematical perspective. Will it work for your particular case is completely different story. The good thing is most of the algorithms which work for metrics will work with pseudometrics as well. The only difference is that you simply "glue together" points which have the same image (f(A)=f(B)), if this is not the issue for your problem - then you can apply this kind of pseudometric in any metric-based reasoning without any problems. In practise, that means that if your f is

computes the sum of squared distances between the input point and the corresponding cluster center

this means that this is actually a distance to closest center (there is no summation involved when you consider a single point). This would mean, that 2 points in two separate clusters are considered identical when they are equally far away from their own clusters centers. Consequently your measure captures "how different are relations of points and their respective clusters". This is a well defined, indirect dissimilarity computation, however you have to be fully aware what is happening before applying it (since it will have specific consequences).

By : lejlot

Video about How to handle self references in Entity Framework code-first?