Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework 4.1 - Segregating an entity into multiple tables (Code-First)
    primarykey
    data
    text
    <p>I'm trying to find out how I can re-use a simple 'Comment' entity type for multiple scenarios where something is 'commentable' in my application.</p> <p>At the moment, I have a couple of entities that a user is able to post comments to. Examples include Blogs, Profiles and Photos - these can all be 'commented' on.</p> <p>I'd like to be able to use the same 'Comment' class for each of these scenarios, but I don't want to end up with one HUGE table full of comments for everything. I figure it would be much more efficient to at least store a table of BlogComments, PhotoComments, and ProfileComments. At the moment, my Comment class looks like this:</p> <pre><code>public class Comment { [Key] public int Id { get; set; } public int ContextId { get; set; } [StringLength(256)] public string Content { get; set; } public DateTime DatePosted { get; set; } public virtual Member Author { get; set; } } </code></pre> <p>Presumably, I'd need the 'ContextId' field to refer to the particular thing being commented on. This Id might be the Id of a Blog, a Profile or a Photo. I was hoping to be able to refer to comments much like a normal ICollection in these classes, and I have some code like this for the Photos as an example:</p> <pre><code>public class Photo { [Key] public int Id { get; set; } [StringLength(48)] public string FileName { get; set; } public virtual Member Owner { get; set; } public virtual ICollection&lt;Comment&gt; Comments { get; set; } } </code></pre> <p>I've been pointed to various articles during my searches, but none really seem relevant to my particular situation. How can I map these comment collections to different tables, and avoid having a comment "super-table"?</p> <p>Any help, pointers or advice would be hugely appreciated :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload