locked
EntitySetName RRS feed

  • Question

  • Having an Entity, is there a call to retrieve the EntitySetName so it can be used in a call like:

     

    aKey = aContext.GetEntityKey(entitySetName, aEntity);

     

    ?

     

    I want to implement a method that can get different entities and I don;t like to pass the EntitySetName string too.

     

    Thanks,

    Ioan

     

    Friday, April 25, 2008 6:40 PM

Answers

  • Well, there are two parts to this:

     

    1) If you are talking about an entity that is not attached to the context, then no, there is no way to accomplish this.  The reason is that the type of the entity doesn't provide enough information to determine the entityset name, because it's possible to have more than one entityset that contains the same entity type.  I like to think of this like types and instanes in code.  I can create a class in c# or vb which defines a new type, but I don't have any instances until I declare a variable of that type of class.  In the EF, you define entity types, and then you declare instances of them in the form of entitysets of that type.  When you later go to add an object to the context or something like that, you need to specify which entityset you want to add to.

     

    2) If you do have something attached to the context, then in beta 3 there aren't a lot of options, but in the upcoming beta it's possible given the object instance to ask the context for the ObjectStateEntry that goes with that object, and from there you can look up the key and the entityset that goes with it.

     

    - Danny

    Saturday, April 26, 2008 4:27 AM
  • The visual designer makes several simplifying assumptions (at least in v1) which are not true in the actual model which the runtime uses.  For v1 you cannot have the same entity type in more than one entityset *in the designer*, but you can write XML files by hand which do this.

     

    - Danny

     

    Tuesday, April 29, 2008 3:12 PM

All replies

  • Well, there are two parts to this:

     

    1) If you are talking about an entity that is not attached to the context, then no, there is no way to accomplish this.  The reason is that the type of the entity doesn't provide enough information to determine the entityset name, because it's possible to have more than one entityset that contains the same entity type.  I like to think of this like types and instanes in code.  I can create a class in c# or vb which defines a new type, but I don't have any instances until I declare a variable of that type of class.  In the EF, you define entity types, and then you declare instances of them in the form of entitysets of that type.  When you later go to add an object to the context or something like that, you need to specify which entityset you want to add to.

     

    2) If you do have something attached to the context, then in beta 3 there aren't a lot of options, but in the upcoming beta it's possible given the object instance to ask the context for the ObjectStateEntry that goes with that object, and from there you can look up the key and the entityset that goes with it.

     

    - Danny

    Saturday, April 26, 2008 4:27 AM
  •  Daniel Simmons - MSFT wrote:

    Well, there are two parts to this:

     

    1) If you are talking about an entity that is not attached to the context, then no, there is no way to accomplish this.  The reason is that the type of the entity doesn't provide enough information to determine the entityset name, because it's possible to have more than one entityset that contains the same entity type. 

     

    Of course, this is the case.

     

    I still cannot imagine how one entity type can be part of more than one entity set (I'm thinking at the Model Designer).

     

    Thanks.

     

    Tuesday, April 29, 2008 2:41 PM
  •  IoanB wrote:

    I still cannot imagine how one entity type can be part of more than one entity set (I'm thinking at the Model Designer).

     

    This is because having the same entity type in multiple entity sets is not supported by the designer tools in the first release. It is supported by the runtime, though, and so our APIs refer to the entity set.

     

    -Diego

    Tuesday, April 29, 2008 3:11 PM
  • The visual designer makes several simplifying assumptions (at least in v1) which are not true in the actual model which the runtime uses.  For v1 you cannot have the same entity type in more than one entityset *in the designer*, but you can write XML files by hand which do this.

     

    - Danny

     

    Tuesday, April 29, 2008 3:12 PM
  • Thanks for the answers. Yeah, the picture is much bigger than I can see after a couple of months digging into EF.

     

     

    Tuesday, April 29, 2008 4:35 PM