0

Say I have two Entities.

public class Category{
    public virtual int Id{get;set;}
    public virtual IList<Post> Posts{get;set;}
}

public class Post{
    public virtual int Id{get;set;}
    public virtual string Title{get;set;}
}

In the Db there's a many-to-many table

CategoryPostRel

  • CategoryId
  • PostId

The category Map then looks like this:

public CategoryMap()
    {   
        HasManyToMany(x => x.Posts)
            .Table("CategoryPostRel")
            .ParentKeyColumn("CategoryId")
            .ChildKeyColumn("PostId");
    }

Ok, but say I only want the Ids from the Posts. So I change my Category entity to look like this.

    public class Category{
    public virtual int Id{get;set;}
    public virtual IList<int> PostIds{get;set;}
}

So now, how do I get the ids with my mapping as the HasManyToMany maps Entities, not columns right?

Note that I can't change the db at all and the many-to-many table has no unique identifier.

    public CategoryMap()
    {   
        HasManyToMany(x => x.PostIds)
            .Table("CategoryPostRel")
            .ParentKeyColumn("CategoryId")
            .ChildKeyColumn("PostId").HowDoIgetTheIds...?
    }
andy
  • 8,775
  • 13
  • 77
  • 122

1 Answers1

1

You could create an entity that models this relationship CategoryPost and do something like this:

public CategoryMap()
{   
    HasMany(x => x.CategoryPostIds)
        .KeyColumn("CategoryId")

}

public CategoryPostMap
{
    CompositeId()
        .KeyProperty(x => x.PostId)
        .KeyProperty(x => x.CategoryId)
}

This is obviously not an ideal solution but it may work.

Cole W
  • 15,123
  • 6
  • 51
  • 85
  • +1 ahh... ok. This sounds good. So, I tried this extra entity approach but because the many-to-many doest have a primary key I couldn't map it. The CompositeId method solves this issue? is that right? – andy Oct 26 '11 at 03:54