List of foreign keys in Entity Framework Core using SQLite

c# entity-framework entity-framework-core sqlite

Question

I want to implement Related Pages structure that one page have a list of other pages related to it. But I could't figure out if it is possible. Now I have the structure like this:

public class Page
{
    public int ID { get; set; }
    public string UrlName { get; set; }
    public List<RelatedPages> RelPages1 { get; set; }
}
public class RelatedPages
{
    public int ID { get; set; }
    public int PageID { get; set; }
    public Page Page { get; set; }
    // I would like this to be the list of related page IDs 
    public List<int> RelPagesID { get; set; }
    public List<Page> RelPages { get; set; }
}

And here is OnModelCreating method in my DbContext:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
            modelBuilder.Entity<RelatedPages>()
                        .HasOne(x => x.Page)
                        .WithMany(x => x.RelPages1)
                        .HasForeignKey(x => x.PageID);

}
1
0
8/8/2016 1:32:59 PM

Popular Answer

If your trying to do a recursive selection such that EF will load all the records in turn for the same table then I don't think this is possible. Otherwise your structure should work fine.

0
8/8/2016 1:44:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow