Skip to content
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

Get TPH Queryable from TPT "ish" implementation with the currently supported feature set? #28919

Closed
TehWardy opened this issue Aug 30, 2022 · 3 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@TehWardy
Copy link

TehWardy commented Aug 30, 2022

Hi EF Team, as always love your work and keep up the good you all do.

I'm looking for what would basically come out of a TPT implementation as I presume would be covered by #247

Prior to this being implemented I have something like a TPT mechanism at the moment but there's no base type in place so these are normal sets for a document management system and I need to get a "view" or read only IQueryable that that I can expose as an OData endpoint read only set set (as a bare minimum, full CRUD would be ideal) ...

Is there any recommendation on how to do this?

Here are my existing entities ...

    [Table("Folders", Schema = "DMS")]
    public class Folder
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        [ForeignKey("App")]
        public int AppId { get; set; }

        [ForeignKey("Parent")]
        public Guid? ParentId { get; set; }

        public string Name { get; set; }
        public string Path { get; set; }
        public App App { get; set; }
        public Folder Parent { get; set; }
        public ICollection<Folder> SubFolders { get; set; }
        public ICollection<File> Files { get; set; }
        public ICollection<FolderRole> Roles { get; set; }
    }

    [Table("Files", Schema = "DMS")]
    [Parent("Folder")]
    public class File
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        [ForeignKey("Folder")]
        public Guid FolderId { get; set; }

        [Required]
        public string Name { get; set; }

        public string Description { get; set; }

        [Required]
        public string Path { get; set; }

        [Required]
        public string MimeType { get; set; }

        public string CreatedBy { get; set; }

        [StringLength(10)]
        public string Size { get; set; }

        public DateTimeOffset CreatedOn { get; set; }

        public Folder Folder { get; set; }

        public ICollection<FileContent> Contents { get; set; }
    }
    
     public class FileSystemObject
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        [ForeignKey("App")]
        public int AppId { get; set; }

        [ForeignKey("Parent")]
        public Guid? ParentId { get; set; }

        public string Name { get; set; }
        public string Path { get; set; }
        public string MimeType { get; set; }

        public virtual App App { get; set; }
        public virtual Folder Parent { get; set; }

        public virtual ICollection<FileSystemObject> Children { get; set; }

        public virtual ICollection<FolderRole> Roles { get; set; }
    }
    

... I was thinking about a "quick and dirty hack" looking something like this method in my DbContext ...

        public IQueryable<FileSystemObject> GetFileSystemObjects()
            => Folders
                .Select(f => new FileSystemObject()
                {
                    Id = f.Id, 
                    ParentId = f.ParentId,
                    AppId = f.AppId, 
                    Name = f.Name, 
                    Path = f.Path
                })
                .Union(Files.Select(f => new FileSystemObject()
                {
                    Id = f.Id,
                    ParentId = f.FolderId,
                    AppId = f.Folder.AppId,
                    Name = f.Name,
                    Path = f.Path
                }));

... something tells me this is not the right way to go though and what about relationships, I can't really map those in LINQ properly I don't think?

Surely there's a smarter way, and given that I only really "need" a read only only set it must help, but Ideally having it full CRUD would standardise it along side the rest of my API layer.

As you can see at the moment the new FileSystemObject type is not a child or parent type for either file or folder, I could define it as such if it helps with the solution proposed,

I'm just looking for guidance here.

Thoughts / ideas please?

@ajcvickers
Copy link
Member

@TehWardy TPT has been available since EF Core 5.0.

@TehWardy
Copy link
Author

Hmmm weird ...
I rad a documentation page that said it only existed in EF6 and was not yet in EF Core ... Ignore me then, I'll go and migrate this to a normal TPT setup.

I assume this is your recommendation (good use case for it)?

Thank you Arthur :)

@ajcvickers
Copy link
Member

@TehWardy I would never recommend TPT. See https://devblogs.microsoft.com/dotnet/announcing-ef7-preview5/

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2022
@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants