forein key reference, with the Entity Framework

0

To have a difficulty doing an insert , because the EF indicates that there is an error in the table reference. But in my config , I indicate what is the foreing key .

My config:

public class TestDriveConfig : EntityTypeConfiguration<TestDrive>
{
    public TestDriveConfig()
    {
        ToTable("TestDrive");
        HasKey(p => p.Id);

        Property(p => p.Id).IsRequired();
        Property(p => p.ExpireDate).IsRequired();
        Property(p => p.OpeningDate).IsRequired();
        Property(p => p.UserId).IsRequired();

        HasRequired(p => p.User).WithMany().HasForeignKey(a => a.UserId);
    }
}

The error that the EF presents, when performing the insert:

  

"Foreign key violation [TestDrive :: UserId]. The key value [2] does not exist in the referenced table." error code: RelationError "

    
asked by anonymous 14.08.2015 / 19:52

0 answers