1

Concept of polymorphic model. For example we have this table comment:

+----+---------------+-------------------+------------------------------------+
| id | commentable_id| commentable_type  |            comments_text           |
+----+---------------+-------------------+------------------------------------+
|  1 |    2          |      youtube      |        youtube rocks, buddy!       |
|  2 |    6          |       vimeo       |        hey, vimeo is better!       |
+--------------------+-------------------+------------------------------------+
  • Where youtube and vimeo refer to the model class Youtube(){…} and class Vimeo(){…} youtube can has many comments, and vimeo can has many comments, and comment belong to youtube and vimeo.
  • If we want to add another model that has many comments in the future we just need to add that in commentable_type without adding more field like something_id into comment table.
  • If we want to add dailymotion, we just need to add dailymotion in commentable_type without adding field dailymotion_id into comment table
  • commentable_id = 2 refer to youtube_id = 2 in youtube table, and commentable_id = 6 refer to vimeo_id = 6 in vimeo table.

In Laravel (PHP) we can simply do like this:

class Youtube extends Eloquent{

    public function comments()
    {
        return $this->morphMany('Comment', 'commentable');
    }
}

class Vimeo extends Eloquent{

    public function comments()
    {
        return $this->morphMany('Comment', 'commentable');
    }
}

class Comment extends Eloquent{

    public function commentable()
    {
        return $this->morphTo();
    }
}

and we can simply access them like this:

$youtube = Youtube::find(1);
$youtube->comments;

$vimeo = Vimeo::find(1);
$vimo->comments;

My Questions are, how to derive the relationship of this ORM in ASP MVC? And how we can easily access them to do CRUD operation?

4
  • This is an ORM question, not an ASP.NET question.. which ORM are you using as each ORM will have different ways to solve this problem? Commented Mar 20, 2014 at 9:37
  • @BiffBaffBoff Maybe my question about ORM is wrong. How to this in ASP MVC? how to define this relationship in ASP MVC? So we can easily access those comments Commented Mar 20, 2014 at 9:41
  • What code do you currently have? I don't know what ORM you're using to access your model(s) so I can't possibly tell you how to access it.. Commented Mar 20, 2014 at 9:46
  • @BiffBaffBoff I'm using EF code first Commented Mar 20, 2014 at 9:50

2 Answers 2

1

I'm not sure if this is entirely what you're looking for, but something like this should suffice:

public abstract class Video
{
    public int Id { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

public class Youtube : Video
{
    /* Other properties */
}

public class Vimeo : Video
{
    /* Other properties */
}

public class Comment
{
    public int Id { get; set; }
    public string CommentText { get; set; }

    public int VideoId { get; set; }

    public virtual Video Video { get; set; }
}

public class VideoContext : DbContext
{
    public DbSet<Youtube> Youtubes { get; set; }
    public DbSet<Vimeo> Vimeos { get; set; }
    public DbSet<Comment> Comments { get; set; }
}

This creates the following structure when querying:

enter image description here

To access it you would have to do something like this:

var context = new VideoContext();

var vimeos = context.Vimeos.Include(x => x.Comments).ToList();
var youtubes = context.Youtubes.Include(x => x.Comments).ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

how do we access them later? is like this possible Youtube.comments , Vimeo.comments ?
See my edit.. take a look at this for some basic information with working with EF and MVC: asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/…
TQ.. this link kinda helpful as well, have been 1 year didn't touch asp weblogs.asp.net/manavi/archive/2010/12/28/…
0

You can use Inheritance http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application

public class Video {
// Id etc.
public virtual IList<Comment> Comments {get; set;}
}

public class Youtube : Video{
// Id etc.
}

public class Vimeo : Video{
// Id etc.
}

public class Comment {
// comment props
 public virtual IList<Video> Videos {get; set;}
}

Comments

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.