I know in entity framework you are able to do something like
public MyObject myObj {get;set;}
public int myObjId {get;set;}
which provides me access to either the key or the corresponding object. However, can you do something like for 1.M associations?
public ICollection<MyObject> myObj {get;set;}
public ICollection<int> myObjIds {get;set;}
Would be very useful if you can.
No, you can’t. At least not in the sense that EF would automatically populate the key collection for you when you load the object from memory (as it is the case with foreign key properties).
But you have two alternative options:
You can create a readonly property:
This requires that you either have already loaded the
myObjcollection or that you use lazy loading.The second way is to load the Ids explicitely when you need them: