2017-06-19 4 views
0

クラスをインスタンス化しようとすると、エンティティフレームワークが奇妙な動作を続けるという問題に直面しています。私はデフォルトのusermanagerの動作を使用して、データベースにデータを格納しようとしています。アプリケーションのユーザークラスで利用可能な通知のリストにその通知を追加する前に、新しい通知のインスタンスを作成し始めると、それらの変更は維持され、エンティティフレームワークからのエラーが処理されます。変更を保持私はここに、クラスのCFをインスタンス化したら、私のコントローラのコードは次のとおりです。Entity Frameworkクラスのインスタンス化の動作

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クラス:

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

私FluentAPIコード:事前に

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

ありがとう!

+0

他のもの、次のように代わりに '+'のinterpolation' '文字列を使用してみてくださいAdding a Name.LastName}友人の招待状を送信しました。 "また、すべてのプロパティを' ctor'に渡すのではなく、 '新しい通知{AssociatedUser = 、Title = }'のような 'Object Initialization'を使用して、 – TheVillageIdiot

+0

このUserManager.FindById()がやっていることと、dbContextが初期化されている場所のコードをポストします。 –

+0

userManager.FindById()は、Microsoftが提供するIDフレームワークの既定のメソッドです。そのクラスのコードはオンラインで入手できます。私は実際に問題がマイクロソフトのコードから来ているとは思っていません。それ以外の場合、フレームワークコードを提供するのが面倒です。 –

答えて

0

AssociatedUserにリンクする外部キーはありません。外部キーを使用してNotificationApplicationUserにリンクするプロパティを追加してみてください。同様にその後

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

は次のように設定を変更してみてください: `$"ユーザー{AddingUser.FirstName} {:離れて

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

これは実際に問題を解決していませんでした –

+0

今何のエラーがありましたか?実際の問題点を特定するのに役立つエラーの詳細を投稿してください。 'ApplicationUser'も同じ' DbContext'クラスで設定されていますか? – TheVillageIdiot

+0

私が得るエラーは次の行にあります:> UserManager.Update(addedUser); エンティティのフレームワークの制約違反があり、関係(Qwirk.Models.Notification_AssociatedUser)の役割(Notification_AssociatedUser_Target)の多重度が1または0..1です。 私が以前に言ったように、私は新しい通知をインスタンス化するときのように思えます変更し、通知テーブルの行を既に作成しているので、問題の原因となった通知テーブルに別の行を作成しようとしているようです。 –

関連する問題