c# - 我在从两个外键引用到两个不同表的复合键中出错

标签 c# asp.net asp.net-mvc ef-code-first

我有 3 个模型用户、ArticleComments 和 UserSubsription .. 我试图将 3 个 Prop 作为复合键,其中两个是对两个不同表的外键引用,但我在尝试启用迁移时出错

用户.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; }
    }
}

用户订阅.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; }
    }
}

最佳答案

基于微软description of the ForeignKeyAttribute :

If you add the ForeigKey attribute to a foreign key property, you should specify the name of the associated navigation property. If you add the ForeigKey attribute to a navigation property, you should specify the name of the associated foreign key(s). If a navigation property has multiple foreign keys, use comma to separate the list of foreign key names

在您的代码中,您没有定义外键属性。您只需定义导航属性。

查看这篇文章以了解如何使用 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; }

    }
}

关于c# - 我在从两个外键引用到两个不同表的复合键中出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36822907/

相关文章:

c# - 使用同一列进行多个计数的 EF Core Linq

c# - 动态添加字段到 Razor 表单

c# - System.Collections 与 System.Collections.ObjectModel

c# - 如何在不实际单击按钮的情况下触发 Button Click 事件?

c# - 将数据库数据插入列表框 C#

c# - 获取 Delete HttpResponseMessage 的属性

asp.net - dbo.TempGetStateItemExclusive3 重复调用

c# - MVC中如何处理jQuery bootgrid请求数据

c# - 调用异步方法而不等待应答

asp.net-mvc - 和 ASP.Net MVC 中的 NTLM