【问题标题】:Code first approach not creating foreign key代码优先方法不创建外键
【发布时间】:2016-10-22 09:00:34
【问题描述】:

我正在尝试使用实体框架代码优先方法创建 1-n 关系。以下是我的课程

public class User
{

    [Key]
    public int UserID { get; set; }


    //public virtual Vote Vote { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Email { get; set; }
    [Required]
    public string Bio { get; set; }
    [Required]
    public string Education { get; set; }
    [Required]
    public string Experience { get; set; }

    public string Password { get; set; }

}

接下来是我的第二节课

public class Vote
{
    [Required]
    [Key]
    public int VoteID { get; set; }

    [ForeignKey("UserID")]
    [Required]
    public virtual User User { get; set; }

    //public virtual int UserID { get; set; }

    [Required]
    public int VoteCount { get; set; }
    [Required]

    public  int UserID { get; set; }


}

用户和投票和 1-n 关系,在我的例子中,我的意思是用户 ID 应该是投票表中的外键。但是当我用以下命令创建数据库时

   enable-migrations -ContextTypeName ProfileOne.PO -Force

   Add-migration PO

   update-database

我没有得到任何外键或列名UserID

任何帮助将不胜感激,为什么我无法达到结果。

【问题讨论】:

  • 这应该使用UserID 创建一个Vote 表。但关系是 1-n(用户有很多票),而不是 1-1。如果没有创建UserID,我们看不到代码中的其他错误。
  • 我相信在这种情况下 EF 期望一对一的关系。根据 EF 版本,该属性可能会被忽略,因为约定在这种关系中使用 PK 作为 FK。
  • 这应该创建外键。我们在这里遗漏了部分谜题。很容易为这类问题创建一个minimal reproducible example

标签: .net asp.net-mvc entity-framework entity-framework-6


【解决方案1】:

尝试使用以下方法:

public class User
{
    [Key]
    public int UserID { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string Email { get; set; }

    [Required]
    public string Bio { get; set; }

    [Required]
    public string Education { get; set; }

    [Required]
    public string Experience { get; set; }

    public string Password { get; set; }        


    //Navigation properties
    public virtual ICollection<Vote> Votes { get; set; }
}


public class Vote
{
    [Key]
    public int VoteID { get; set; } 

    [Required]
    public int VoteCount { get; set; }  


    //Foreign key for User
    [Required]
    public int UserID { get; set; } 

    //Navigation properties
    public virtual User User { get; set; }
}


有关更多信息,请查看Entity Relationships。希望这会有所帮助...

【讨论】:

    【解决方案2】:

    修改你的类看起来像:

    public class User
    {
        [Key]
        public int UserID { get; set; }
    
        [Required]
        public string Name { get; set; }
    
        [Required]
        public string Email { get; set; }
    
        [Required]
        public string Bio { get; set; }
    
        [Required]
        public string Education { get; set; }
    
        [Required]
        public string Experience { get; set; }
    
        public string Password { get; set; }
    
        public virtual Vote Vote { get; set; }
    }
    
    public class Vote
    {
        [Required]
        [Key]
        public int VoteID { get; set; }
    
        [Required]
        public int VoteCount { get; set; }
    
        [Required]
        public  int UserID { get; set; }
    
        //[ForeignKey("UserID")]
        //[Required]
        public virtual User User { get; set; }
    }
    

    两个实体之间的关系由虚拟属性定义,您对其进行了注释。

    【讨论】:

    • 没有。属性是多余的(命名约定做同样的事情),但肯定没有错。也不需要 virtual 修饰符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多