实体框架类的实例化行为 [英] Entity Framework class instantiation behavior

查看:66
本文介绍了实体框架类的实例化行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,当我尝试实例化任何类时,我的实体框架一直表现异常.我正在尝试使用默认的 usermanager 行为将数据存储在数据库中.一旦我开始实例化一个新通知,然后再将该通知添加到应用程序用户类中可用的通知列表中,它将保留这些更改并处理来自实体框架的错误,该错误指出存在多重性问题,我该如何告诉实体框架不要一旦实例化一个类,持久化更改cf,这是我的控制器代码:

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";
}

我的通知类:

[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;
    }
}

我的ApplicationUser类:

my ApplicationUser class:

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

我的FluentAPI代码:

my FluentAPI code :

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

提前谢谢!

推荐答案

您没有任何用于链接AssociatedUser的外键.尝试添加属性以使用外键将Notification链接到ApplicationUser.喜欢

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);

这篇关于实体框架类的实例化行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆