Skip to content

Can't create resource with relationship in Entity-Resource separation mode #445

Closed
@milosloub

Description

@milosloub

Consider following:

Entities:

    public class Client : Identifiable
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Parent Parent { get; set; }
        public int ParentId { get; set }
    }
    public class Parent: Identifiable
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

Resources:

    public class ClientResource : Identifiable
    {
        [Attr]
        public string FirstName { get; set; }
        [Attr]
        public string LastName { get; set; }
        [HasOne]
        public ParentResource Parent { get; set; }
    }
    public class ParentResource: Identifiable
    {
        [Attr]
        public string FirstName { get; set; }
        [Attr]
        public string LastName { get; set; }
    }

Three Layer flow (with Automapper Profile)
1) Controller - JsonApiController<ClientResource>
2) Service - EntityResourceService<ClientResource, Client, int>
3) Repository - DefaultEntityRepository<Client>

If I POST request like this:

{
    "data": {
        "attributes": {
            "first-name": "Homer",
            "last-name": "Simpson"
        },
        "relationships": {
            "parent": {
                "data": {
                    "type": "parents",
                    "id": "1"
                }
            }
        },
        "type": "clients"
    }
}

the InvalidOperationException is thrown, because:
"The entity type 'ParentResource' was not found. Ensure that the entity type has been added to the model."
This exception points to line 234 of DefaultEntityRepository and it's AttachHasOnePointers method.

Do you have any idea, how to handle this? Does this mean, that Repository should be responsible for proper resolve of navigation property type?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions