1

For example we have profile and organisation. Both have articles.

    public class Article
    {
        public int Id { get; set; }
        public string Title { get; set; }
    }

    public class Profile
    {
        public int Id { get; set; }
        public string Email { get; set; }
        public virtual ICollection<Article> Articles { get; set; }
    }

    public class Organisation
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Article> Articles { get; set; }
    }

In this way Article should have two kinds of parent so it should have something like parent type to be able to access a parent when you select articles directly.

    public class Article
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public int ParentId { get; set; }
        public ArticleParentType Parent { get; set; }
    }
  1. Is it possible to map it using Entity Framework?
  2. Is it a good idea to do it?
  3. What is the best practice for storing this kind of data?

1 Answer 1

1
public class Article
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int ParentId { get; set; }
    public ArticleParentType Parent { get; set; }
}
  1. Is it possible to map it using Entity Framework?
  2. Is it a good idea to do it?

Possible yes but not a good idea. The underlying Database can't use a foreign key for Parentid. It would be slow.

  1. What is the best practice for storing this kind of data?

A simple approach, with 2 Nullable parents and without CascadeOnDelete:

public class Article
{
    public int Id { get; set; }
    public string Title { get; set; }

    public virtual Profile Profile { get; set; }
    public virtual Organisation Organisation { get; set; }
}

Alternatively you could use inheritance for Article, ie class OrganisationArticle : Article {}

Sign up to request clarification or add additional context in comments.

2 Comments

For example I decided to use "A simple approach, with 2 Nullable parents".. but there are many other questions. For example there is a post -> comments structure. Comment has two creators - User and Organisation. Should I add .Include() to each creator when I select posts from DB? What to do if you have 10 parents?
How exactly to refactor the class model?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.