【发布时间】: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 值应该是一个逗号分隔的外键属性名称列表。
标签: c# asp.net asp.net-mvc ef-code-first