0

I have a User Model

public class User {
    public Guid IdGuid {get;set;}
    public string Name {get;set;}
    public virtual List<User> Friends {get;set;}
}

And I have second model

public class UserFriendship{
    public Guid FriendShipIdGuid {get;set;}
    public Guid UserIdGuid {get;set;}
    public Guid FriendIdGuid {get;set;}
}

I have FluentApi code OnModelcreating in context.

modelBuilder.Entity<User>().HasMany(u => u.Friends).WithMany().Map(c => {
    c.MapLeftKey("UserIdGuid"); 
    c.MapRightKey("FriendIdGuid");
    c.ToTable("UserFriendship");
    });

Problem : I already have a UserFriendship class model. And I want to create relation about users to this model. And I am doing it with FluentApi.

But after FluentApi code executed, FriendShipIdGuid is removed from UserFriendship Table.

I need to create one to many relation for Users, and save it to table UserFriendship and map it already existing class model name with UserFriendship which element of dbset. Can you help me please ?

As a summary, I already have UserFriendship class. And this class contains FrientshipGuidId (Key), UserIdGuid, FriendIdGuid. I need to map, User class one to many to itself. And I want to save this relation on UserFriendship class. And I want to use Userfriendship as dbset also.

I have

public DbSet<UserFriendship> Friendships { get;set; }

in my context.

I am using FluentApi ToTable function to create one to many relation table. But FluentApi code delete FriendshipIdGuid field.

Ras
  • 89
  • 1
  • 9
  • Are you trying to use Code First with an existing table? – mostruash Apr 19 '14 at 23:21
  • @mostruash Yes. Existing table creating by fluent. But i need to use this UserFriendship as dbset also. I will use this friendship data for User class property. This property is List Friends . Is that possible ? I need to generate User.Friends list from UserFriendship table and i need also use UserFriendship as DbSet. – Ras Apr 19 '14 at 23:29

1 Answers1

0

It's not possible. You can't use a List<UserFriendship> in User class either because sometimes a User is a user and other times a User is a friend, unless you have duplicates in your UserFriendship table like this:

Friendship table:          |
----------------------------

User / Friend
-------------
  A  /   B
  B  /   A
-------------

First change User and UserFriendship classes as follows:

class UserFriendship 
{
    public Guid FriendShipIdGuid {get;set;}
    public Guid UserIdGuid {get;set;}
    public Guid FriendIdGuid {get;set;}

    public User User {get;set;}
    public User Friend {get;set;}
}

class User 
{
    public Guid IdGuid {get;set;}
    public string Name {get;set;}

    // Only if you will allow duplicates in UserFriendship
    public virtual List<UserFriendship> Friendships {get;set;}
}

I don't remember the exact syntax/methods of Fluent API but you will get the idea:

modelBuilder.Entity<User>()
  .HasKey(u => u.IdGuid)
  .ToTable("User");

modelBuilder.Entity<UserFriendship>()
  .HasKey(uf => uf.FriendShipIdGuid)
  .ToTable("UserFriendship");

modelBuilder.Entity<UserFriendship>()
  .HasRequired(uf => uf.User)
  .WithMany(u => u.Friendships)       // Only if you allow duplicates
  //.WithMany()                         // Otherwise
  .HasForeignKey(uf => uf.UserIdGuid);

modelBuilder.Entity<UserFriendShip>()
   .HasRequired(uf => uf.Friend)
   .WithMany()
   .HasForeignKey(uf => uf.FriendIdGuid);
mostruash
  • 4,169
  • 1
  • 23
  • 40
  • what do you mean when you said duplicates ? for example 1 friends with 2 and 2 friend with 1 . Do you mean that ? Another think, you commented //.WithMany() . If i dont want to use duplicates, should i use delete this .WithMany() line ? – Ras Apr 20 '14 at 00:02
  • Yes, I mean that. If you don't want to use duplicates, use `.WithMany()`. Delete `List Friendships` from `User` class. I suggest you to use duplicates. It will make your life easier. Please accept as answer if this helped you. – mostruash Apr 20 '14 at 00:09
  • I could nt solve it with fluentApi :( Trying another ways. Thanks for help. – Ras Apr 20 '14 at 13:58