我从两个外键引用到两个不同的表中创建复合键时出错
Posted
技术标签:
【中文标题】我从两个外键引用到两个不同的表中创建复合键时出错【英文标题】:ihave an error with making composite key from two foreign key references to two different tables 【发布时间】:2016-04-24 12:09:01 【问题描述】:我有 3 个模型用户、ArticleComments 和 UserSubsription .. 我试图将 3 个道具作为复合键,其中两个是对两个不同表的外键引用,但我在尝试启用迁移时出错
用户.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;
文章评论.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.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 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;
【问题讨论】:
你遇到了什么错误? “WebAppInternetApp.Models.ArticleComments”类型的属性“ArticleID”上的 ForeignKeyAttribute 无效。在依赖类型“WebAppInternetApp.Models.ArticleComments”上找不到外键名称“JobArticle”。 Name 值应该是一个逗号分隔的外键属性名称列表。 【参考方案1】:基于微软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;
【讨论】:
如果我删除外键,我收到此消息“WebAppInternetApp.Models.UserSubscription: : EntityType 'UserSubscription' 没有定义键。定义此 EntityType 的键。UserSubscriptions: EntityType: EntitySet 'UserSubscriptions' 是基于没有定义键的“UserSubscription”类型。" 但它会为用户 ID 和文章 ID 创建两个属性! 我在尝试此 WebAppInternetApp.Models.UserSubscription: 时收到此消息::EntityType 'UserSubscription' 没有定义密钥。定义此 EntityType 的键。 UserSubscriptions:EntityType:EntitySet 'UserSubscriptions' 基于没有定义键的类型 'UserSubscription'。以上是关于我从两个外键引用到两个不同的表中创建复合键时出错的主要内容,如果未能解决你的问题,请参考以下文章
复合求和:我想创建一个复合查询,它从两个不同的表中获取两列的单独总和,然后对它们求和