简体   繁体   中英

Entity framework relations with identity

I am working on a website using ASP and entity framework and identity.

I am actually facing a problem where an entity framework doesn't add entries into the database tables even doe I am adding classes to my model.

The actual problem I am facing just hits a specific subclass of the identity model which I declared as the following :

public partial class ApplicationUser : IdentityUser
{
           public virtual List<Friend> Friends { get; set; }
}

Where friends are declared as the following :

[Table("Friends")]
public class Friend
{
    [Key, Column(Order = 1)]
    public string AddingUser { get; set; }
    [Key, Column(Order = 2)]
    public string AddedUser { get; set; }

    public string Pseudo { get; set; }
    public DateTime FriendsSince { get; set; }
    public bool IsInvitation { get; set; }
    public bool IsAccepted { get; set; }
    public bool IsBloqued { get; set; }
    public int NumberOfUnreadenMessages { get; set; }

    public virtual List<Message> FriendShipMessages { get; set; }
    [ForeignKey("AddingUser")]
    public virtual ApplicationUser AddingFriend { get; set; }
    [ForeignKey("AddedUser")]
    public virtual ApplicationUser AddedFriend { get; set; }

    public Friend()
    {
        FriendShipMessages = new List<Message>();
    }

    public Friend(ApplicationUser AddingUser, ApplicationUser AddedUser)
    {
        AddedFriend = AddedUser;
        AddingFriend = AddingUser;
        this.AddingUser = AddingUser.Id;
        IsInvitation = false;
        IsAccepted = false;
        IsBloqued = false;
        FriendsSince = DateTime.Now;
        NumberOfUnreadenMessages = 0;
        FriendShipMessages = new List<Message>();
    }
}

I did add to the EF modelbuilder this rule to map the application user to the friends table:

// Mapping Friends And Bloqueds
modelBuilder.Entity<ApplicationUser>()
     .HasMany(c => c.Friends)
     .WithRequired(c => c.AddingFriend)
     .WillCascadeOnDelete(false);

In my controller part to add a user I am using the following code:

public string AddFriend(string AddedUserId)
{
    var AddedUser = UserManager.FindById(AddedUserId);
    var AddingUser = UserManager.FindById(User.Identity.GetUserId());
    AddingUser.Friends.Add(new Friend(AddingUser, AddedUser) { 
           IsInvitation = true });
    return "Friend was added successfully";
}

Where is the problem why is my controller not adding effectively the entry to the database knowing that during debugging sessions I do have the good entities going into the controller and it also effectively create the instance of the friend class correctly.

Thank you in advance for your response.

Changes to entity objects don't get persisted to a database until you actually tell your context to do it. My preferred method of this would be to use the context directly instead of the UserManager :

var addedUser = db.Users.Single(u => u.Id == addedUserId);
var addingUser = db.Users.Single(u => u.Id == User.Identity.GetUserId());

addingUser.Friends.Add(new Friend(addingUser, addedUser) 
{ 
    IsInvitation = true;
});

db.SaveChanges();

But this might work (I'm assuming UserManager calls db.SaveChanges internally) if you really want to use the UserManager :

var addedUser = UserManager.FindById(addedUserId);
var addingUser = UserManager.FindById(User.Identity.GetUserId());

addingUser.Friends.Add(new Friend(addingUser, addedUser) 
{ 
    IsInvitation = true;
});

UserManager.UpdateUser(addingUser);

Side note: I've also changed the casing of your parameters and variables to match with common convention. I highly recommend learning that way and sticking with it, particularly if you intend to work with other developers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM