locked
Use Sequence to generate a key RRS feed

  • Question

  • Hello,

    we have an Oracle database where there are sequences to generate the values for keys for new records. Entity Framework allows me to call an Oracle-sequence easily, so I always have the new key at hand. What I cannot do is to use this key and put its value into my entity. I can do it manualy of course, but I'd like do do something like:

          foreach ( ObjectStateEntry entry in context.ObjectStateManager.GetObjectStateEntries( EntityState.Added ))
          {
            string keyPropertyName = entry.EntityKey.EntityKeyValues[0].Key;
    

    but that does not work: The docs state clearly that EntityKey is not set for entities with the Added state.

    So the question is: how can I find the primary key property in my entity, so that I can set its value? I want to do this in the SaveChanges method (or

    ObjectContext_SavingChanges) to make the solution generic.

    Thanks

    daProgramma

    Thursday, November 22, 2012 10:54 AM

Answers

All replies

  • Hi,

    go through this link below, hope it will help you out.

    http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/550cd4de-7299-4b35-a3d9-6c00ea243131

    -Arun

    Thursday, November 22, 2012 12:47 PM
  • Hello Arun,

    thanks for the effort answering my question. Unfortunaltely, although very interesting by itself, the article does not deal with my question:

         how can I find the primary key property in my entity, so that I can set its value?

    There shoud be some way to query the Entity Model to find the property that is the PK for a given entity. I can then set its value via reflection.

    Regards
    daProgramma

    Thursday, November 22, 2012 2:41 PM
  • Hi daProgramma,

    Since your database generate the primary keys (server generate them), you are not required to assign the values explicitly. After you call SaveChanges, server generates the key value for them automatically.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Friday, November 23, 2012 8:29 AM
  • Hello Alexander,

    my database does NOT generate the primary keys, and YES, I am definitely required to assign the values explicitely.

    FYI: Oracle has the concept of a "Sequence". Sequences are database objects of their own, and can be used to make primary keys for tables. To provide a new record with a key, someone has to query the Sequence and use the result for the key. One can use a trigger, but in our solution they use NHibernate which is configured so that it uses the proper Sequence when it comes to make a new record.

    We want to try to use EF with this given database. I personaly find it a pity that EF does not allow to register a "generator function" to allow a client to provide to the EF proper key values when needed. I can simulate this by overloading SaveChanges and there I can manually provide values for the keys.

    It would be nice if I could somehow find the property of my entity that is the key so that I set its value in SaveChanges.

    Regards
    daProgramma

    Friday, November 23, 2012 2:09 PM
  • Hi daProgramma,

    I got your point. Thank you for explanation. Entities that are in the "Added" state (newly created, and not yet saved) have what is called a temporary EntityKey. This EntityKey has an EntitySet, but no key values because the entity is still Added. The key cannot become a real EntityKey (an EntityKey with values) until you save the Entity.

    What you can do, and here is the work around I think you should use, is to assign your generated value to the primary keys of the entity directly. Please refer to this page: http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/c23b5a2f-89ef-4b8e-9811-ad9fae5716e6

    If you want to find key property name, please check the Finding primary key property names part on this page: http://blog.oneunicorn.com/2012/05/03/the-key-to-addorupdate/

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Friday, December 7, 2012 2:01 AM
    Wednesday, November 28, 2012 7:55 AM
  • Hello Alexander,

    >Entities that are in the "Added" state (newly created, and not yet saved) have what is called a temporary EntityKey. This EntityKey has an EntitySet, but no key values because the entity is still Added. The key cannot become a real EntityKey (an EntityKey with values) until you save the Entity.

    Indeed. I observed this myself and therefore I wanted to set the key field (I have only one) of my entities MANUALLY in SaveChanges-Method. As you indicated, this post:

    http://blog.oneunicorn.com/2012/05/03/the-key-to-addorupdate/

    has the recipe how to find the name(s) of the key property/properties of any given entity in a generic way. Can be used beautifully in SaveChanges-Method to provide the key value(s) if the entity is in added state.

    For German readers, this one might be of help:

    http://blog.technophobie.de/2012/05/ef-entitaten-finden-der-primary-keys.html

    At last, I have one more (minor) question: Do I use standard-reflection techniques to set the key property, or does EF have some method for that?

    Regards
    daProgramma

    Thursday, November 29, 2012 1:51 PM