Mapping a collection of value types in the Entity Framework

Similar question: Matching a string collection to 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 db, "RoleActionMapping" with columns, "RoleID" and "Action". "RoleID" is the external key point for the role table, and "Action" is varchar. It seems I can’t set up the EF mapping to populate the Role class using RoleActions.

Any ideas on how to achieve this? Thank!

+5
source share
1 answer

EF does not offer such a display.

, :

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; }
}

Role , IEnumerable<string>, Actions.

, M: N Role Action.

+3

All Articles