1

私は2つのクラス、UserとFollowを持っていますが、私は同じクラスに対して2つの関係を作成しましたが、私は外部キーのペアで一意性を制約する必要があります。Entity Frameworkコードファーストで仮想列にインデックスを追加する方法?

インデックスを追加しようとしましたが、動作しません。インデックスは移行コードに追加されません。ユーザーとフォローユーザーは仮想だからです。

仮想外部キーのペアに一意性を追加するにはどうすればよいですか?

ありがとうございます。

注:Azure Mobile App Serviceを使用していて、主キーフィールドが自動的に追加されます。

public class Following : EntityData 
{ 

    [Index("IX_FriendshipUniqueness", 1, IsUnique = true)] 
    public virtual User User { get; set; } 

    [Index("IX_FriendshipUniqueness", 2, IsUnique = true)] 
    public virtual User FollowingUser { get; set; } 

    [StringLength(10)] 
    public string Status { get; set; } 

} 

public class User : EntityData 
{ 
    public User() 
    { 
     this.Followings = new HashSet<Following>(); 
     this.Followers = new HashSet<Following>(); 
    } 


    [InverseProperty("User")] 
    public virtual ICollection<Following> Followings { get; set; } 

    [InverseProperty("FollowingUser")] 
    public virtual ICollection<Following> Followers { get; set; } 

} 

答えて

1

私は数日前に書いた関係のアドバイスのコラムをお読みください - それはあなたがAzureのモバイルアプリ要求制約することができます方法を理解するのを助ける: - (EF6が可能にもかかわらず、このような制約https://shellmonger.com/2016/05/27/30-days-of-zumo-v2-azure-mobile-apps-day-26-relationship-advice/

ショートバージョンを確かにそうだとは言っていません - 私は決定的な声明を出すのはEFではありません)、Azure Mobile Appsはこれらの制約を実行する能力を提供していません。あなたのクライアントコードでそれを強制する必要があります。

関連する問題