我遇到了一個類似的問題,與現有的問題具有相同的標題,但略有不同。ReferentialConstraint中的依賴屬性映射到商店生成的列。列:'SpecificationID'
我得到的錯誤:
A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'SpecificationID'.
當我嘗試保存到數據庫:
var doc = new Specification();
doc.ProjectComponentID = spec.ProjectComponentID;
doc.Description = spec.Description;
db.Specifications.Add(doc);
db.SaveChanges();
當我創造了我使用的種子的方法來映射1對1或無關係數據庫:
modelBuilder.Entity<Specification>()
.HasRequired(pc => pc.ProjectComponent)
.WithOptional(s => s.Specification);
我2個相關的實體是:
public class ProjectComponent
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProjectComponentID { get; set; }
public int ProjectID { get; set; }
public virtual Project Project { get; set; }
public int ProjectPhaseID { get; set; }
public virtual ProjectPhase Phase { get; set; }
... other properties ...
public Nullable<int> SpecificationID { get; set; }
public virtual Specification Specification { get; set; }
}
public class Specification
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SpecificationID { get; set; }
public int ProjectComponentID { get; set; }
public virtual ProjectComponent ProjectComponent { get; set; }
public string Description { get; set; }
}
我需要能夠將規範添加到PRojectComponent。使用ProjectComponentID提供規範,但自動生成規範ID僅當嘗試保存規範時,我纔會在創建數據庫時遇到任何錯誤。