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

How to map a nested entity while both are attached to any other single Entity in EF Core 3 #22714

Closed
ajcvickers opened this issue Sep 24, 2020 · 2 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@ajcvickers
Copy link
Member

Moved from discussions.

Question

From @hamidyaseen

To store Notes objects/entities in Folder and it's sub Folder. Actually, a "Catergory" Entity have many "Folder"/s (say parent), which itself can have further "Folder" (say child folder), but no more level of sub-folder. So relation is as
Category => Folder/s (parent folders)=> Folder/s. (child folders). that's it.
How to map/link a "Note" entity, which could be in a Parent-Folder or in any Child Folder?

Replies

From @ajcvickers

@hamidyaseen Please post the C# code for the types that you want to map.


From @hamidyaseen

public class Category  {
        public Category() {
              Childrens = new List<Folder>();
        }		
        public int Id { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        
        public List<Folder> Childrens { get; set; }
}
public class Folder {
        public Folder() {
	       Notes = new List<Note>();
               Children = new List<SubFolder>();
        }		
        public int Id { get; set; }
        public string Name { get; set; }
        public int Type { get; set; }

	public List<Note> Notes { get; set; }
        public List<SubFolder> Children { get; set; }
}
public class SubFolder {
	public SubFolder() {
		Notes = new List<Note>();            
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public int Type { get; set; }
		
	public List<Note> Notes { get; set; }
}
	
public class Note  {
        public int Id { get; set; }
        public string Title { get; set; }
        public DateTime Deadline { get; set; }
        public DateTime Reminder { get; set; }
        public string Detail { get; set; }
	public int ContainerId { get; set;} //Could be folder/subFolder id 		
}

FoldersStructure
Quest:- As Folder & SubFolder are almost the same entities (except no more folders under SubFolder). For simplicity, considering them two separate entities, so EFcore mapped to two DB tables. right. As a Note entity could be in a Folder or SubFolder. But mapped Note Db table have two foreign keys, one for Folder and one for SubFolder. Instead of a single foreign key for containing folder key.

@ajcvickers
Copy link
Member Author

@hamidyaseen I don't think there is a way to map this using EF Core. The ContainerId FK could reference either a Folder or SubFolder, and EF has no way of determining which one.

@AndriySvyryd Would this be possible after #10140?

@AndriySvyryd
Copy link
Member

@ajcvickers No, #10140 won't help here

@hamidyaseen You can derive Folder from SubFolder, so it will inherit the relationship with Note

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Oct 2, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 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