entity-frameworkfluent-nhibernateone-to-many

Map a one to many reference in Entity Framework as collection of ids instead of actual entity reference


In Fluent Nhibernate I can map a multiple entity references 1-M as a list of int ids :

    public class User{
     List<int> LicensesIds{
      get;
      set;
    }

Instead of

Public class User{
 ICollectoin<License> Licenses{
  get;
  set;
 }
}

Which is how Entity Framework maps it. In the fluent mapping when I query the DB I get it with a list of ids instead of a list of entities.

Is there away I can map this the same way in Entity Framework ?

The reason I need it is because I am using the Repository pattern and have created a repository for each entity in the domain : Repository and I do not want to define what relations to query and get with an entity when I query the DB by using LINQ or by lazy binding in the middle of a property call without me being aware that this property call is now going to fetch data from the DB. I want to bring the entities manually by querying the relevant repository.

Thx ,

James


Solution

  • It is not possible. You must always have list of licences in your entity if you want EF to handle loading for you (you can than expose second property doing just projection to list of integers). If you want just list of int implement custom logic in your repository which will execute additional query and fill it.