2016-04-24 111 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创建两个属性! –