title: One-to-one mapping in multiple tables
body: <p>I'm trying to solve one puzzle, but with no luck so far.</p>
  
  <p>I have an article (or blog post) and comment entities, they both have content. In order to support lazy loading for content (there is no need to load the content when I need to display a list of articles or comments) I decided to move content to separate table and organize one-to-one mapping. Here is an example of what I think:</p>
  
  <pre><code>public class Content {
      [Key]
      public int ID { get; set; }
      public string RawContent { get; set; }
      // a bunch of scalar properties, like content type and so on
  }
  
  public class BlogArticle {
      [Key]
      public int ID { get; set; }
      public int ContentID { get; set; }
      [ForeignKey(nameof(ContentID)]
      public virtual Content Text { get; set; }
      // other properties related to BlogArticle
  }
  
  public class Comment {
      [Key]
      public int ID { get; set; }
      public int ContentID { get; set; }
      [ForeignKey(nameof(ContentID)]
      public virtual Content Text { get; set; }
      // other properties related to comment
  }
  &lt;...&gt;
  </code></pre>
  
  <p>From first look it seems ok: I can create blog articles, comments and attach content (at first I insert content, obviously). Update works as well. However, deletion doesn't work: when I delete blog article or comment, content is not deleted (but I want to delete it when blog article or comment are deleted, not opposite).</p>
  
  <p>From what I understand my biggest issue because of relationship direction: in my case, <code>Content</code> entity is principal end and <code>BlogArticle</code> and <code>Comment</code> are dependent ends. In order to solve the puzzle, I need to change principal/dependent relationship. Again, from what I understand in order to change relationship direction I need to have a foreign key in <code>Content</code> entity and use fluent API to describe who is parent (principal) and who is child (dependent) in one-to-one relationship. Since many tables (there might be other entities with content property) are pointing to <code>Content</code> table, it doesn't seem very easy. Am I correct in my understanding?</p>
  
  <p>One possible solution I could imagine is to create multiple foreign keys in <code>Content</code> table and point to each related table:</p>
  
  <pre><code>public class Content {
      [Key]
      public int ID { get; set; }
      public string RawContent { get; set; }
      // foreign keys
      public int BlogArticleID { get; set; }
      public int CommentID { get; set; }
      public int WebWidgetID { get; set; }
      // other foreign keys if necessary
  }
  </code></pre>
  
  <p>probably, foreign keys must be nullable (because only single foreign key is used at once). Then use Entity Framework fluent API to describe relationship directions and organize cascade delete. For me it looks ugly, but I have no other ideas.</p>
  
  <p>My question: is my proposed solution good/reliable? Are there other options I can look at?</p>
  
  <p>Thanks in advance!</p>