Skip to content

Foreign keys incorrectly named in Manual configuration section #3628

@kristijan-boskovic

Description

@kristijan-boskovic

In Manual configuration section (Data annotations subsection), there is a code snippet with Post and User classes. In Post class, foreign key properties for Author and Contributor navigation properties are named AuthorUserId and ContributorUserId, which is incorrect if there are no [ForeignKey] annotations with those foreign key property names included in it. If no [ForeignKey] annotations are used, those foreign key properties should be named AuthorId and ContributorId, to make sure Entity Framework Core connects them with foreign keys declared in the Model Snapshot.

I found this out by getting the SQLite Error 1: 'no such table' exception when trying to execute SaveChanges(). In one of my entity classes, I had these properties declared, which both refer to the Person entity class:

...
public int? OwnerPersonId { get; set; }
public Person Owner { get; set; }

public int? CurrentUserPersonId { get; set; }
public Person CurrentUser { get; set; }
...

I checked out the Model Snapshot class and I noticed that foreign keys were named OwnerId and CurrentUserId, thus being disconnected from my OwnerPersonId and CurrentUserPersonId properties. After I renamed those properties to OwnerId and CurrentUserId, and recreated the database, SaveChanges() executed successfully.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions