6

Similar question: Mapping collection of strings with Entity Framework or Linq to SQL

I have a role class:

public class Role
{
    public int RoleID { get; set; }
    public virtual IList<string> Actions { get; set; }
}

I have a mapping table in the db, "RoleActionMapping" with the columns, "RoleID" and "Action". "RoleID" is a foreign key point to the Role table, and "Action" is a varchar. I cannot seem to setup my EF mapping to populate the Role class with the RoleActions.

Any ideas on how to achieve this? Thanks!

Community
  • 1
  • 1
reustmd
  • 3,513
  • 5
  • 30
  • 41

1 Answers1

3

EF doesn't offer such mapping.

If you want to map it you must use:

public class Role
{
  public int RoleID { get; set;}
  public virtual IList<RoleAction> Actions { get; set; } // you should initialize collection
}

public class RoleAction
{
  public int ActionId { get; set; } // PK must be defined or entity is readonly
  public string Action { get; set; }
  public virtual Role { get; set; }
}

You can further extend Role class to provied not mapped property returning IEnumerable<string> which will internally select data from Actions property.

But once you follow this approach you should consider model it as M:N relation between Role and Action entity.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Too bad, I wish it did. Thanks for the solution. – reustmd Mar 03 '11 at 20:41
  • @manu08 For a perspective on *why* "collections of value objects" might not be offered, see http://mhinze.com/2009/01/10/there-is-never-a-collection-of-value-objects/ (This is in general Domain Driven Design terms, not Entity Framework specific). – Nathan May 05 '14 at 15:49