ReferentialConstraint中的C#/Entity框架相关属性映射到存储生成的列

ReferentialConstraint中的C#/Entity框架相关属性映射到存储生成的列,c#,entity-framework,C#,Entity Framework,我正在使用实体框架开发一个基于web的应用程序。我最初是从一个现有数据库中以“代码优先”的方式生成模型的。但是,从那时起,我修改了数据库结构,并相应地更新了实体框架类。其中一个手动编辑包括添加一个类“Employee” 每当我尝试执行此代码时: using (ApplicationModel dbContext = new ApplicationModel()) { Applicant applicant = dbContext.Appli

我正在使用实体框架开发一个基于web的应用程序。我最初是从一个现有数据库中以“代码优先”的方式生成模型的。但是,从那时起,我修改了数据库结构,并相应地更新了实体框架类。其中一个手动编辑包括添加一个类“Employee”

每当我尝试执行此代码时:

        using (ApplicationModel dbContext = new ApplicationModel())
        {
            Applicant applicant = dbContext.Applicants.First(a => a.ID == applicantID);
            applicant.Hired = true;

            // Transfer Data to New Hire
            Employee newHire = new Employee();
            newHire.Applicant = applicant;
            newHire.CellPhone = (applicant.CellPhone == null) ? "" : applicant.CellPhone;
            newHire.City = applicant.City;
            newHire.CurrentLocation = applicant.Store;
            newHire.Email = applicant.Email;
            newHire.FirstName = applicant.FirstName;
            newHire.LastName = applicant.LastName;
            newHire.Login = applicant.Email;
            newHire.MiddleInitial = applicant.MiddleInitial;
            newHire.Password = applicant.SSN;
            newHire.Phone = applicant.Phone;
            newHire.State = applicant.State;
            newHire.StreetAddress = applicant.StreetAddress;
            newHire.Zip = applicant.Zip;

            dbContext.Employees.Add(newHire);
            dbContext.SaveChanges();
        }
        return RedirectToAction("Index");
    }
}
我收到以下错误:

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'EmployeeID'.
员工模型如下所示:

public partial class Employee
{

  [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int EmployeeID { get; set; }

    public int ApplicantID { get; set; }

    public int CurrentLocationID { get; set; }

    public bool CompletedNewEmpData { get; set; }

    public bool ModuleOneComplete { get; set; }

    public bool ModuleTwoComplete { get; set; }

    public string DOB { get; set; }


    public string Login { get; set;  }

    public string Password { get; set; }

    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }

    [StringLength(2)]
    public string MiddleInitial { get; set; }
    [Column(TypeName = "text")]
    public string StreetAddress { get; set; }

    [StringLength(50)]
    public string City { get; set; }

    [StringLength(2)]
    public string State { get; set; }

    [StringLength(5)]
    public string Zip { get; set; }

    [StringLength(60)]
    public string Email { get; set; }

    [StringLength(20)]
    public string Phone { get; set; }

    [StringLength(20)]
    public string CellPhone { get; set; }

    public string SSN { get; set; }


    public string MaritalStatus { get; set; }

    public string Residency { get; set; }

    public char Sex { get; set; }

    public string Race { get; set; }

    public string AlienNumber { get; set; }


    public string NewHireNumber { get; set; }

    public string ResponsibleManagerName { get; set; }

    public bool Rehired { get; set; }

    public decimal? PayRate { get; set; }

    public bool SaintLouisResident { get; set; }

    public string FedStatus { get; set; }

    public string StateStatus { get; set; }

    public decimal? FederalAllowances { get; set; }

    public decimal? StateAllowances { get; set; }

    public decimal? FederalAdditional { get; set; }

    public decimal? StateAdditional { get; set; }

    public string PaySelection { get; set; }

    public string PaySelectionSignature { get; set; }

    public string PaySelectionDate { get; set; }

    public string PaySelectionAccountType { get; set; }

    public string PaySelectionAccountNumber { get; set; }

    public string PaySelectionRoutingNumber { get; set; }

    public string PaySelectionCheck { get; set; }

    public string MoneyNetworkInitials { get; set; }

    public string DirectDepositInitials { get; set; }
    public string WorkCompSignature { get; set; }

    public string WorkCompSignatureDate { get; set; }

    public string JobDescriptionSignature { get; set; }

    public string JobDescriptionSignatureDate { get; set; }

    public string MemoSignature { get; set; }

    public string MemoSignatureDate { get; set; }

    public string CriminalCheckSignature { get; set; }

    public string CriminalCheckDate { get; set; }

    public string AgeAcknowledgmentSig { get; set; }

    public string AgeAcknowledgmentSigDate { get; set; }

    public string HarassmentPolicySignature { get; set; }

    public string HarassmentPolicyDate { get; set; }

    public string HandbookSignature { get; set; }

    public string HandbookSignatureDate { get; set; }

    public string DatingPolicySignature { get; set; }

    public string DatingPolicyDate { get; set; }

    public string UniformReceiptDate { get; set; }

    public string UniformReceiptSignature { get; set; }

    public string UniformItemList { get; set; }
    public decimal? UniformValue { get; set; }

    public virtual Applicant Applicant { get; set; }

    public virtual Store CurrentLocation { get; set; }

}
public partial class ApplicationModel : DbContext
{
    public ApplicationModel()
        : base("name=ApplicationModel")
    {
    }

    public virtual DbSet<Applicant> Applicants { get; set; }
    public virtual DbSet<DistrictManager> DistrictManagers { get; set; }
    public virtual DbSet<Division> Divisions { get; set; }
    public virtual DbSet<Education> Educations { get; set; }
    public virtual DbSet<EmergencyContact> EmergencyContacts { get; set; }
    public virtual DbSet<PropertyType> PropertyTypes { get; set; }
    public virtual DbSet<QuestionOption> QuestionOptions { get; set; }
    public virtual DbSet<Question> Questions { get; set; }
    public virtual DbSet<QuestionType> QuestionTypes { get; set; }
    public virtual DbSet<Reference> References { get; set; }
    public virtual DbSet<Store> Stores { get; set; }
    public virtual DbSet<UserLevel> UserLevels { get; set; }
    public virtual DbSet<User> Users { get; set; }
    public virtual DbSet<Employee> Employees { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Applicant>()
            .Property(e => e.FirstName)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.LastName)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.MiddleInitial)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.SSN)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.StreetAddress)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.City)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.State)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.Zip)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.Email)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.Phone)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.CellPhone)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.HowHear)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.Score)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .HasMany(e => e.EmergencyContacts)
            .WithRequired(e => e.Applicant)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<DistrictManager>()
            .Property(e => e.Phone)
            .IsUnicode(false);

        modelBuilder.Entity<DistrictManager>()
            .Property(e => e.Email)
            .IsUnicode(false);

        modelBuilder.Entity<Division>()
            .Property(e => e.Description)
            .IsUnicode(false);

        modelBuilder.Entity<Education>()
            .Property(e => e.Name)
            .IsUnicode(false);

        modelBuilder.Entity<Education>()
            .Property(e => e.Location)
            .IsUnicode(false);

        modelBuilder.Entity<Education>()
            .Property(e => e.GPA)
            .HasPrecision(3, 1);

        modelBuilder.Entity<Education>()
            .Property(e => e.SchoolType)
            .IsUnicode(false);


        modelBuilder.Entity<EmergencyContact>()
            .Property(e => e.Name)
            .IsUnicode(false);

        modelBuilder.Entity<EmergencyContact>()
            .Property(e => e.Address)
            .IsUnicode(false);

        modelBuilder.Entity<EmergencyContact>()
            .Property(e => e.HomePhone)
            .IsUnicode(false);

        modelBuilder.Entity<EmergencyContact>()
            .Property(e => e.WorkPhone)
            .IsUnicode(false);

        modelBuilder.Entity<EmergencyContact>()
            .Property(e => e.Relationship)
            .IsUnicode(false);

        modelBuilder.Entity<Employee>()
         .HasRequired(e => e.Applicant)
         .WithOptional(e => e.Employee);

        modelBuilder.Entity<PropertyType>()
            .Property(e => e.Description)
            .IsUnicode(false);

        modelBuilder.Entity<QuestionOption>()
            .Property(e => e.Letter)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<QuestionOption>()
            .Property(e => e.OptionText)
            .IsUnicode(false);

        modelBuilder.Entity<Question>()
            .Property(e => e.QuestionText)
            .IsUnicode(false);

        modelBuilder.Entity<QuestionType>()
            .Property(e => e.Description)
            .IsUnicode(false);

        modelBuilder.Entity<QuestionType>()
            .HasMany(e => e.Questions)
            .WithOptional(e => e.QuestionType1)
            .HasForeignKey(e => e.QuestionType);

        modelBuilder.Entity<Reference>()
            .Property(e => e.Name)
            .IsUnicode(false);

        modelBuilder.Entity<Reference>()
            .Property(e => e.Phone)
            .IsUnicode(false);

        modelBuilder.Entity<Reference>()
            .Property(e => e.ReferenceType)
            .IsUnicode(false);

        modelBuilder.Entity<Store>()
            .Property(e => e.Email)
            .IsUnicode(false);

        modelBuilder.Entity<Store>()
            .Property(e => e.Phone)
            .IsUnicode(false);

        modelBuilder.Entity<Store>()
            .Property(e => e.Address)
            .IsUnicode(false);

        modelBuilder.Entity<UserLevel>()
            .Property(e => e.Description)
            .IsUnicode(false);

        modelBuilder.Entity<UserLevel>()
            .HasMany(e => e.Users)
            .WithRequired(e => e.UserLevel1)
            .HasForeignKey(e => e.UserLevel)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<User>()
            .HasMany(e => e.DistrictManagers)
            .WithRequired(e => e.User)
            .WillCascadeOnDelete(false);
    }
}
}

ApplicationModel类如下所示:

public partial class Employee
{

  [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int EmployeeID { get; set; }

    public int ApplicantID { get; set; }

    public int CurrentLocationID { get; set; }

    public bool CompletedNewEmpData { get; set; }

    public bool ModuleOneComplete { get; set; }

    public bool ModuleTwoComplete { get; set; }

    public string DOB { get; set; }


    public string Login { get; set;  }

    public string Password { get; set; }

    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }

    [StringLength(2)]
    public string MiddleInitial { get; set; }
    [Column(TypeName = "text")]
    public string StreetAddress { get; set; }

    [StringLength(50)]
    public string City { get; set; }

    [StringLength(2)]
    public string State { get; set; }

    [StringLength(5)]
    public string Zip { get; set; }

    [StringLength(60)]
    public string Email { get; set; }

    [StringLength(20)]
    public string Phone { get; set; }

    [StringLength(20)]
    public string CellPhone { get; set; }

    public string SSN { get; set; }


    public string MaritalStatus { get; set; }

    public string Residency { get; set; }

    public char Sex { get; set; }

    public string Race { get; set; }

    public string AlienNumber { get; set; }


    public string NewHireNumber { get; set; }

    public string ResponsibleManagerName { get; set; }

    public bool Rehired { get; set; }

    public decimal? PayRate { get; set; }

    public bool SaintLouisResident { get; set; }

    public string FedStatus { get; set; }

    public string StateStatus { get; set; }

    public decimal? FederalAllowances { get; set; }

    public decimal? StateAllowances { get; set; }

    public decimal? FederalAdditional { get; set; }

    public decimal? StateAdditional { get; set; }

    public string PaySelection { get; set; }

    public string PaySelectionSignature { get; set; }

    public string PaySelectionDate { get; set; }

    public string PaySelectionAccountType { get; set; }

    public string PaySelectionAccountNumber { get; set; }

    public string PaySelectionRoutingNumber { get; set; }

    public string PaySelectionCheck { get; set; }

    public string MoneyNetworkInitials { get; set; }

    public string DirectDepositInitials { get; set; }
    public string WorkCompSignature { get; set; }

    public string WorkCompSignatureDate { get; set; }

    public string JobDescriptionSignature { get; set; }

    public string JobDescriptionSignatureDate { get; set; }

    public string MemoSignature { get; set; }

    public string MemoSignatureDate { get; set; }

    public string CriminalCheckSignature { get; set; }

    public string CriminalCheckDate { get; set; }

    public string AgeAcknowledgmentSig { get; set; }

    public string AgeAcknowledgmentSigDate { get; set; }

    public string HarassmentPolicySignature { get; set; }

    public string HarassmentPolicyDate { get; set; }

    public string HandbookSignature { get; set; }

    public string HandbookSignatureDate { get; set; }

    public string DatingPolicySignature { get; set; }

    public string DatingPolicyDate { get; set; }

    public string UniformReceiptDate { get; set; }

    public string UniformReceiptSignature { get; set; }

    public string UniformItemList { get; set; }
    public decimal? UniformValue { get; set; }

    public virtual Applicant Applicant { get; set; }

    public virtual Store CurrentLocation { get; set; }

}
public partial class ApplicationModel : DbContext
{
    public ApplicationModel()
        : base("name=ApplicationModel")
    {
    }

    public virtual DbSet<Applicant> Applicants { get; set; }
    public virtual DbSet<DistrictManager> DistrictManagers { get; set; }
    public virtual DbSet<Division> Divisions { get; set; }
    public virtual DbSet<Education> Educations { get; set; }
    public virtual DbSet<EmergencyContact> EmergencyContacts { get; set; }
    public virtual DbSet<PropertyType> PropertyTypes { get; set; }
    public virtual DbSet<QuestionOption> QuestionOptions { get; set; }
    public virtual DbSet<Question> Questions { get; set; }
    public virtual DbSet<QuestionType> QuestionTypes { get; set; }
    public virtual DbSet<Reference> References { get; set; }
    public virtual DbSet<Store> Stores { get; set; }
    public virtual DbSet<UserLevel> UserLevels { get; set; }
    public virtual DbSet<User> Users { get; set; }
    public virtual DbSet<Employee> Employees { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Applicant>()
            .Property(e => e.FirstName)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.LastName)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.MiddleInitial)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.SSN)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.StreetAddress)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.City)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.State)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.Zip)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.Email)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.Phone)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.CellPhone)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.HowHear)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .Property(e => e.Score)
            .IsUnicode(false);

        modelBuilder.Entity<Applicant>()
            .HasMany(e => e.EmergencyContacts)
            .WithRequired(e => e.Applicant)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<DistrictManager>()
            .Property(e => e.Phone)
            .IsUnicode(false);

        modelBuilder.Entity<DistrictManager>()
            .Property(e => e.Email)
            .IsUnicode(false);

        modelBuilder.Entity<Division>()
            .Property(e => e.Description)
            .IsUnicode(false);

        modelBuilder.Entity<Education>()
            .Property(e => e.Name)
            .IsUnicode(false);

        modelBuilder.Entity<Education>()
            .Property(e => e.Location)
            .IsUnicode(false);

        modelBuilder.Entity<Education>()
            .Property(e => e.GPA)
            .HasPrecision(3, 1);

        modelBuilder.Entity<Education>()
            .Property(e => e.SchoolType)
            .IsUnicode(false);


        modelBuilder.Entity<EmergencyContact>()
            .Property(e => e.Name)
            .IsUnicode(false);

        modelBuilder.Entity<EmergencyContact>()
            .Property(e => e.Address)
            .IsUnicode(false);

        modelBuilder.Entity<EmergencyContact>()
            .Property(e => e.HomePhone)
            .IsUnicode(false);

        modelBuilder.Entity<EmergencyContact>()
            .Property(e => e.WorkPhone)
            .IsUnicode(false);

        modelBuilder.Entity<EmergencyContact>()
            .Property(e => e.Relationship)
            .IsUnicode(false);

        modelBuilder.Entity<Employee>()
         .HasRequired(e => e.Applicant)
         .WithOptional(e => e.Employee);

        modelBuilder.Entity<PropertyType>()
            .Property(e => e.Description)
            .IsUnicode(false);

        modelBuilder.Entity<QuestionOption>()
            .Property(e => e.Letter)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<QuestionOption>()
            .Property(e => e.OptionText)
            .IsUnicode(false);

        modelBuilder.Entity<Question>()
            .Property(e => e.QuestionText)
            .IsUnicode(false);

        modelBuilder.Entity<QuestionType>()
            .Property(e => e.Description)
            .IsUnicode(false);

        modelBuilder.Entity<QuestionType>()
            .HasMany(e => e.Questions)
            .WithOptional(e => e.QuestionType1)
            .HasForeignKey(e => e.QuestionType);

        modelBuilder.Entity<Reference>()
            .Property(e => e.Name)
            .IsUnicode(false);

        modelBuilder.Entity<Reference>()
            .Property(e => e.Phone)
            .IsUnicode(false);

        modelBuilder.Entity<Reference>()
            .Property(e => e.ReferenceType)
            .IsUnicode(false);

        modelBuilder.Entity<Store>()
            .Property(e => e.Email)
            .IsUnicode(false);

        modelBuilder.Entity<Store>()
            .Property(e => e.Phone)
            .IsUnicode(false);

        modelBuilder.Entity<Store>()
            .Property(e => e.Address)
            .IsUnicode(false);

        modelBuilder.Entity<UserLevel>()
            .Property(e => e.Description)
            .IsUnicode(false);

        modelBuilder.Entity<UserLevel>()
            .HasMany(e => e.Users)
            .WithRequired(e => e.UserLevel1)
            .HasForeignKey(e => e.UserLevel)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<User>()
            .HasMany(e => e.DistrictManagers)
            .WithRequired(e => e.User)
            .WillCascadeOnDelete(false);
    }
}
public分部类ApplicationModel:DbContext
{
公共应用程序模型()
:base(“name=ApplicationModel”)
{
}

公共虚拟数据库集),无法找到我的问题的任何答案。请提供任何指导。

查看它是否有帮助请检查
EmployeeID
表中是否有
Employee
属性作为外键关系目标,而主键属性位于其他表中。它可能涉及与的一对一关系表主键设置为其他表主键的外键。