我正在使用Entity Framework开发基于Web的应用程序。我最初从现有数据库生成了“Code First”模型。但是,我从那时起修改了数据库结构,并相应地更新了Entity Framework classess。其中一个手动编辑包括添加一个类“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; }
}
}
ApplicationModel类如下:
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);
}
}
}
虽然,我并不否认这是一个明显的,简单的错误的真正可能性,我已经环顾四周(包括这里:A dependent property in a ReferentialConstraint is mapped to a store-generated column)并找不到任何问题的答案。任何指导都将不胜感激。