Reference of forein key, with the Entity Framework

Asked

Viewed 82 times

0

I’m having a hard time making one Insert, because the FE indicates that there is error in the table reference. But, in my config, I state 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 RU presents when performing the Insert:

"Foreign key Violation [Testdrive :: Userid]. The key value [2] does not exist in the referenced table [vw__usuario :: code].. Error code: Relationerror"

  • 1

    Does your database already have data? It seems to me that this error comes from trying to create an external key pointing to an item that does not exist.

  • I tested working with tests, I insert data, then insert into this table.

  • 1

    I think you entered an external key value 2 for user before creating or changing the interface. The RU cannot create the interface because this external key would point to a line that does not exist.

  • You can put both Models in your question?

  • Solved the problem? Otherwise, you can put the class code in the question to try to help you?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.