Skip to content

ResourceObjectAdapter converts relationship but does not set foreign key #1823

@bjornharrtell

Description

@bjornharrtell

SUMMARY

A resource model might have explicitly mapped foreign keys, like the Post entity at https://learn.microsoft.com/en-us/ef/core/modeling/relationships#mapping-relationships-in-ef-core. Typically both the foreign key and and navigation property are expected to be not null if the relationship is required.

When ResourceObjectAdapter converts from document to entity it sets navigation property from relationship but not foreign key property. It will be peristed ok but is unexpectedly null until retrieved from database again. This can cause confusement when for example extending EntityFrameworkCoreRepository and iteract with the entity before persisting. I have encountered this when creating entities with relations using atomic operations, not sure it is relevant elsewhere.

Would it make sense to resolve foreign key mapping and set it in this case?

VERSIONS USED

  • JsonApiDotNetCore version: 5.9.1
  • ASP.NET Core version: 8.0.21
  • Entity Framework Core version: 9.0.10
  • Database provider: Npgsql.EntityFrameworkCore.PostgreSQL 9.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions