2016-04-24 8 views
2

3つのモデルユーザー、ArticleCommentsおよびUserSubsription ..私は3つのpropを複合キーとして作成しようとしています。2つの異なるテーブルへの2つの外部キー参照からコンポジットキーを作成する際にエラーが発生しました

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace WebAppInternetApp.Models 
{ 
    public class ArticleComments 
    { 
     [Required] 
     public string Comment { set; get; } 
     [Key] 
     [Column(Order = 0)] 
     public DateTime CommentTime { set; get; } 
     [Key] 
     [Column(Order = 1)] 
     [ForeignKey("User")] 
     public virtual User UserID { set; get; } 
     [Key] 
     [Column(Order = 2)] 
     [ForeignKey("JobArticle")] 
     public virtual JobArticle ArticleID { set; get; } 
    } 
} 

UserSubsription.Cを有効に移行

User.CSを

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Web; 
using WebAppInternetApp.Models; 

namespace WebAppInternetApp.Models 
{ 
    public class User 
    { 

     public User() 
     { 
      this.WriterIDs = new HashSet<JobArticle>(); 
      this.AdminIDs = new HashSet<JobArticle>(); 
      this.UserIDs = new HashSet<ArticleComments>(); 
      this.UserIDss = new HashSet<UserQuestion>(); 
      this.UseerIDsss = new HashSet<UserSubscription>(); 
     } 

     [Key] 
     public int UID { get; set; } 

     [Required] 
     [StringLength(20, MinimumLength = 2)] 
     public string FName { set; get; } 

     [Required] 
     [StringLength(20, MinimumLength = 2)] 
     public string LName { set; get; } 

     [Required] 
     [DataType(DataType.EmailAddress)] 
     public string Email { set; get; } 

     [Required] 
     [StringLength(25, MinimumLength = 8)] 
     public string Password { set; get; } 

     [Required] 
     public bool Status { set; get; } 

     [Required] 
     [Phone] 
     public string PhoneNo { set; get; } 

     [Column(TypeName = "image")] 
     public byte[] UserImg { set; get; } 

     public virtual ICollection<JobArticle> WriterIDs { set; get; } 
     public virtual ICollection<JobArticle> AdminIDs { set; get; } 
     public virtual ICollection<ArticleComments> UserIDs { set; get; } 
     public virtual ICollection<UserQuestion> UserIDss { set; get; } 
     public virtual ICollection<UserSubscription> UseerIDsss { set; get; } 

     public virtual UserType usertypeIDs { set; get; } 



    } 
} 

記事Comments.CSしようとしているときが、私はエラーを得ましたS

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Web; 
using WebAppInternetApp.Models; 

namespace WebAppInternetApp.Models 
{ 
    public class UserSubscription 
    { 
     [Key] 
     [Column(Order = 0)] 
     [ForeignKey("User")] 
     public virtual User UserIDs { set; get; } 

     [Key] 
     [Column(Order = 1)] 
     [ForeignKey("JobCategory")] 
     public virtual JobCategory SubscriptID { set; get; } 
    } 
} 
+0

どのようなエラーがありますか? – malkassem

+0

タイプ 'WebAppInternetApp.Models.ArticleComments'のプロパティ 'ArticleID'のForeignKeyAttributeが無効です。依存キータイプ 'JobArticle'は、依存タイプ 'WebAppInternetApp.Models.ArticleComments'で見つかりませんでした。 Name値は、外部キーのプロパティ名をコンマで区切ったリストでなければなりません。 –

答えて

0

マイクロソフトdescription of the ForeignKeyAttributeの基本:

外部キープロパティにForeigKey属性を追加する場合、 は、関連するナビゲーションプロパティの名前を指定する必要があります。 ForeigKey属性をナビゲーションプロパティに追加する場合は、 に関連する外部キーの名前を指定する必要があります。ナビゲーション プロパティは、複数の外部キーを持っている場合は、あなたのコードで

外部キー名のリストを区切るために、カンマを使用し、あなたは外部キープロパティを定義しません。ナビゲーションプロパティのみを定義します。

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Web; 
using WebAppInternetApp.Models; 

namespace WebAppInternetApp.Models 
{ 
    public class UserSubscription 
    { 
     [Key] 
     [Column(Order = 0)] 
     [ForeignKey("User")] 
     public int UserIDs { set; get; } 

     [Key] 
     [Column(Order = 1)] 
     [ForeignKey("JobCategory")] 
     public int SubscriptID { set; get; } 

     // those are the navigation properties 
     public virtual User User { set; get; } 
     public virtual JobCategory JobCategory { set; get; } 
    } 
} 

そしてArticleCommentsは、このようなことができます:

Understanding ForeignKey attribute in entity framework code first

は、例えば、あなたのUserSubscriptionは次のようになります。

チェックこの記事はForeignKeyの属性を使用する方法を確認するには:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace WebAppInternetApp.Models 
{ 
    public class ArticleComments 
    { 
     [Required] 
     public string Comment { set; get; } 

     [Key] 
     [Column(Order = 0)] 
     public DateTime CommentTime { set; get; } 

     [Key] 
     [Column(Order = 1)] 
     [ForeignKey("User")] 
     public int UserID { set; get; } 

     [Key] 
     [Column(Order = 2)] 
     [ForeignKey("JobArticle")] 
     public int ArticleID { set; get; } 

     public virtual User User { set; get; } 
     public virtual JobArticle JobArticle { set; get; } 

    } 
} 
+0

外部キーを削除するとこのメッセージが表示されるWebAppInternetApp.Models.UserSubscription :: EntityType 'UserSubscription'にはキーが定義されていません UserSubscriptions:EntityType:EntitySet 'UserSubscriptions'は 'UserSubscription' " –

+0

答えには – malkassem

+0

という例を追加するコードを追加しましたが、useridとarticle idの2つのプロパティが作成されます。 –

関連する問題