简体   繁体   中英

Entity Framework class instantiation behavior

I am facing a problem where my entity framework keeps behaving weirdly when I try to instantiate any class. I am trying to use the default usermanager behaviour to store data in my database. Once I start instantiating a new notification before it adds that notification to the list of notifications available in the application user class it persists those changes and process an error from entity framework saying that there is a multiplicity problem, how can I tell entity framework to not persist changes once I instantiate a class cf here is my controller code :

public string AddFriend(string AddedUserId)
{
    var AddedUser = UserManager.FindById(AddedUserId);
    var AddingUser = UserManager.FindById(User.Identity.GetUserId());
    var friendship = new Friend(AddingUser, AddedUser) { IsInvitation = true };
    AddingUser.Friends.Add(friendship);
    AddedUser.Notifications.Add(new Notification(AddingUser, "Friend Invitation", 
                   "The user " + AddingUser.FirstName + " " + AddingUser.LastName + 
                   " Sent you a friend invitation", friendship));
    UserManager.Update(AddedUser);
    UserManager.Update(AddingUser);
    return "Friend was added successfully";
}

my Notification class :

[Table("Notifications")]
public class Notification
{
    [Key]
    public int NotificationId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    [Required]
    public ApplicationUser AssociatedUser { get; set; }
    public Friend AssociatedFrienship { get; set; }
    public GroupMember AssociatedGroup { get; set; }
    public ChannelMember AssociatedChannel { get; set; }
    public Message AssociatedMessage { get; set; }
    public bool Consulted { get; set; }

    public Notification()
    {
    }

    public Notification(ApplicationUser associatedUser, string title, string content, Friend associatedFriend = null, GroupMember associatedGroup = null, ChannelMember associatedChannel = null, Message associatedMessage = null)
    {
        AssociatedUser = associatedUser;
        Title = title;
        Content = content;
        AssociatedChannel = associatedChannel;
        AssociatedGroup = associatedGroup;
        AssociatedFrienship = associatedFriend;
        AssociatedMessage = associatedMessage;
        Consulted = false;
    }
}

my ApplicationUser class:

public partial class ApplicationUser : IdentityUser
{
    public virtual List<Notification> Notifications { get; set; }
}

my FluentAPI code :

modelBuilder.Entity<Notification>()
            .HasRequired(c => c.AssociatedUser)
            .WithMany(c => c.Notifications);

Thanks in advance!

You don't have any foreign key to link AssociatedUser . Try adding a property to link Notification to ApplicationUser using foreign key. Like

public class Notification
{
    //rest of properties

    //change int to whatever type your primary key is in 
    //ApplicationUser class. I have Guid for example.
    public int ApplicationUser AssociatedUserId {get;set;}
}

Then try modifying configuration like this:

   modelBuilder.Entity<Notification>()
        .HasRequired(c => c.AssociatedUser)
        .WithMany(c => c.Notifications)
        .HasForeignKey(n=>n.AssociatedUserId);

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