Foreign Key being duplicated

时间:2017-11-08 22:06:47

标签: entity-framework ef-code-first ef-migrations

My Class:

   public class UserProcess : BaseModel
    {
        [Key]
        public int UserProcessID { get; set; }

        [Required]
        [ForeignKey("Building")]
        public int BuildingID { get; set; }

        public Building Building { get; set; }

        [Required]
        [ForeignKey("Process")]
        public int ProcessID { get; set; }

        public Process Process { get; set; }

        [Required]
        [ForeignKey("UserProfile")]
        public int UserProfileID { get; set; }

        public UserProfile UserProfile { get; set; }

        public virtual ICollection<UserForm> UserForms { get; set; }

        public UserProcess()
        {
            UserForms = new HashSet<UserForm>();
        }
    }

When I create the database, the table I get has a column "UserProfileID", and a UserProfile_UserProfileID column. When I seed data to it, the UserProfileID is the column being used, and the UserProfile_UserProfileID column is null. Why is this column being created?

Here are the base model and the UserProfile class as requested.

public abstract class BaseModel : IAuditInfo
    {
        [Required]
        public DateTime CreatedOn { get; set; }

        [Required]
        public DateTime ModifiedOn { get; set; }

        [Required]
        public String CreatedBy { get; set; }

        [Required]
        public String ModifiedBy { get; set; }
    }
}

public class UserProfile : BaseModel
{
    [Key]
    public int UserProfileID { get; set; }

    [Required]
    public String FirstName { get; set; }

    [Required]
    public String LastName { get; set; }

    public String EmployeeID { get; set; }

    public String LicenseNumber { get; set; }

    public String Position { get; set; }        

    public bool? Active { get; set; }

    //[Required]
    //[ForeignKey("ApplicationUser")]
    //public String AppUserID { get; set; }

    public ApplicationUser ApplicationUser { get; set; }

    [ForeignKey("Division")]
    public int? DivisionID { get; set; }

    public virtual Division Division { get; set; }

    public virtual ICollection<Building> Buildings { get; set; }

    public virtual ICollection<UserProcess> UserProcesses { get; set; }

    public UserProfile()
    {
        Buildings = new HashSet<Building>();
    }

}

0 个答案:

没有答案