2016-04-24 103 views
2

我有3個模型的用戶,ArticleComments和UserSubsription ..即時嘗試使3道具作爲組合鍵,其中兩個是外鍵引用兩個不同的表但我得到的錯誤嘗試啓用的遷移從兩個外鍵引用到兩個不同的表使複合鍵錯誤

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

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小號

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無效。在依賴類型「WebAppInternetApp.Models.ArticleComments」中找不到外鍵名'JobArticle'。名稱值應該是逗號分隔的外鍵屬性名稱列表。 –

回答

0

基礎上微軟description of the ForeignKeyAttribute

如果添加ForeigKey屬性外鍵屬性,您應該 指定關聯的導航屬性的名稱。如果 將ForeigKey屬性添加到導航屬性,您應該 指定關聯的外鍵的名稱。如果導航 酒店有多個外鍵,用逗號分隔的 外鍵的名稱

在你的代碼清單,你不定義外鍵屬性。您只定義導航屬性。

檢查這篇文章,看看如何使用ForeignKey的屬性:

Understanding ForeignKey attribute in entity framework code first

例如,您UserSubscription應該是這個樣子:

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可能是這樣的:

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'沒有定義密鑰定義此EntityType的密鑰 UserSubscriptions:EntityType:EntitySet'UserSubscriptions'基於類型'UserSubscription'沒有定義密鑰 「 –

+0

在答案中添加了代碼 – malkassem

+0

,但它會爲用戶ID和文章ID創建兩個屬性! –

相關問題