数据库中添加的代码优先EF中的外键重复

时间:2018-11-25 10:36:35

标签: c# entity-framework code-first

我为用户表提供了一个类实体:

[Table("Users", Schema = "Membership")]
public class User
{
    public User()
    {
    }

    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    [Required]
    [StringLength(450)]
    [Index(IsUnique = true)]
    public string Username { get; set; }

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

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

    public int? StateID { get; set; }
    [ForeignKey("StateID")]
    public virtual Place.State State { get; set; }

 }

并且与课程状态有关

[Table("States", Schema = "Place")]

public class State
{
    public State()
    {

    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }

    [Required(ErrorMessage = "Please enter name")]
    public string Name { get; set; }
}

但是在更新数据库后,我看到添加了新的额外键State_Id,我的外键在代码中定义了StateID,但是您可以看到最终表在表数据库中有2个外键。 users table image

0 个答案:

没有答案