0

I'm having a similar problem to existing questions with teh same title, but its slightly different.

I get the error:

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'SpecificationID'.

When I try to save to database:

var doc = new Specification();
doc.ProjectComponentID = spec.ProjectComponentID;
doc.Description = spec.Description;
db.Specifications.Add(doc);

db.SaveChanges();

When I created the database I used the seed method to map a 1 to 1 or none relationship:

modelBuilder.Entity<Specification>()
            .HasRequired(pc => pc.ProjectComponent)
            .WithOptional(s => s.Specification);

My 2 relevant entities are:

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; }

}

I need to be able to add a specification to a PRojectComponent. providing the specification with the ProjectComponentID but auto generating the SpecificationID

I don't get any error on creating the DB, only when trying to save the Specification.

1 Answer 1

0

So I found "A" solution. But I'm not convinced its the "best" solution.

Apparently code-first EF requires the primary keys to be shared in a one to zero or one relationship.

So to solve this issue I have removed the foreign keys on each entity:

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 virtual Specification Specification { get; set; }

}

public class Specification
{
    public int SpecificationID { get; set; }
    public virtual ProjectComponent ProjectComponent { get; set; }

    public string Description { get; set; }
}

and I modified the Fluent API to an inverse of the previous:

modelBuilder.Entity<ProjectComponent>()
            .HasOptional(pc => pc.Specification)
            .WithRequired(s => s.ProjectComponent);

now when saving to the database, I have no issues and it usses the ProjectComponentID foreign key as the SpecificationID primary key:

var comp = db.ProjectComponents.Find(spec.SpecificationID);
comp.Specification = new Specification {Description = spec.Description};

db.SaveChanges();

Please comment if anyone has a better solution... but this is the only way I could find to solve it as a pure code-first managed DB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.