-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
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.
- ID: 24b2b50e-35f8-9e0b-8ce5-840e1ea816e6
- Version Independent ID: 9e0e6e2c-48fa-76c3-3bd9-813a6df22645
- Content: Relationships - EF Core
- Content Source: entity-framework/core/modeling/relationships.md
- Product: entity-framework
- Technology: entity-framework-core
- GitHub Login: @AndriySvyryd
- Microsoft Alias: avickers