Skip to content

Inheritance support #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sebafelis opened this issue Oct 28, 2019 · 6 comments
Closed

Inheritance support #598

sebafelis opened this issue Oct 28, 2019 · 6 comments

Comments

@sebafelis
Copy link

Description

Entity Framework Core Inheritance is not fully support now. It’s support only if for each inherited type is created separeate DBSet in DbContext. But if inheritance is define by FluentAPI then we can’t use this because Resource name is presently inseparable with type property in returning JSON.

Solution

I suggest adding a second string parameter (entityName) to ResourceAttribute and to AddResource method in ContextGraphBuilder. It allows to separate resource name and entity type and provides support for EF Core inheritance.

Environment

  • JsonApiDotNetCore Version: v4.0.0-alpha3
  • Entity Framework Core Versions: 2.2
@maurei
Copy link
Member

maurei commented Oct 29, 2019

Can you provide us with a code example of the involved models and dbcontext you're trying to use with JsonApiDotNetCore which is currently not supported?

@sebafelis
Copy link
Author

sebafelis commented Oct 29, 2019

Here is an example:

public class Blog : Identifiable
{
        [Attr]
        public string Url { get; set; }
}
public class RssBlog : Blog
{
        [Attr]
        public string RssUrl { get; set; }
}
public class AppDbContext : DbContext
{
        public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
        { }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<RssBlog>()
                .HasBaseType<Blog>();
        }
        public DbSet<Blog> Blogs { get; set; }
}

In this case is not possible to Post\Patch a row of type RssBlog by JsonApiDotNetCore.

@maurei
Copy link
Member

maurei commented Oct 29, 2019

Can you share your AddJsonApi( .. ) call from your startup?

@sebafelis
Copy link
Author

services.AddJsonApi(options =>
{
                options.Namespace = "api/v1";
                options.DefaultPageSize = 10;
                options.IncludeTotalRecordCount = true;
                options.BuildResourceGraph((builder) =>
                {
                    builder.AddResource<Blog>("blogs");
                    builder.AddResource<RssBlog>("blogs");
                });
}, mvcBuilder);

This was referenced Nov 1, 2019
@maurei
Copy link
Member

maurei commented Nov 1, 2019

I have reproduced the issue and am working on it :-)

I have been unable to reproduce the issue in the latest commit at the develop branch, see acceptance tests in #610. This build will be available in an alpha release next week.

@maurei maurei closed this as completed Nov 1, 2019
@sebafelis
Copy link
Author

I have checked the alpha 4 version now. It is a bit better (the type is no longer associated with the resource name), but it is still not possible to achieve the result which I want to. I have prepared an example that (I hope so) illustrates what I want to achieve. The code is available in here.
In my project I need one controller that will support types inheriting from one basic type. This is because I know that the number of inheriting types will increase with the development of the project, but I want to avoid the API changes (by adding a new controller for a new type).
In the created example it is possible to read and delete resource object with an inherited type (eg StringUserField) using UserFieldsController, but you cannot add and update information about a given entity by UserFieldsController which is unintuitive.
Also, the relationship links returned by UserControllerdo not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants