locked
get primery key before savechanges? RRS feed

  • Question

  • is possible get current primery key before savechanges?

    post entity:postid(key),postcontent(nvarchar(1000),postfilename(nvarchar(100)))

     [HttpPost]
            public ActionResult Create(Post post)
            {
            post.PostFileName = key??????????;
            _repository.Add(post);
             _repository.SaveChanges();
            }


    Wednesday, March 13, 2013 10:56 AM

Answers

  • Hi Meysam;

    If the primary key is being generated by the database, which I suspect, then no. You must first do SaveChanges and immediately after that the entity in local memory will have the new primary key available to you.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, March 13, 2013 3:32 PM
  • Hi Meysam.

    One work-around could be to simulate a Sequence as Oracle does.

    In this way, you should create a "Sequence" table in your database, adding a Table_Name as PK and a Table_Id as a numeric field for the current sequence value related with this table name.

    Then, you could create a procedure where you may ask for the next sequence you need. Something like, Get_NextSequence("Posts"). This procedure should increment the sequence value in "Sequence" for the "Post" record, and returns this value.

    Finally, you just must to assign this value to your key field before savechanges.

    Hope this could be helpful for you.

    Regards,

    JA Reyes.

     


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Wednesday, March 13, 2013 3:50 PM

All replies

  • Hi Meysam;

    If the primary key is being generated by the database, which I suspect, then no. You must first do SaveChanges and immediately after that the entity in local memory will have the new primary key available to you.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, March 13, 2013 3:32 PM
  • Hi Meysam.

    One work-around could be to simulate a Sequence as Oracle does.

    In this way, you should create a "Sequence" table in your database, adding a Table_Name as PK and a Table_Id as a numeric field for the current sequence value related with this table name.

    Then, you could create a procedure where you may ask for the next sequence you need. Something like, Get_NextSequence("Posts"). This procedure should increment the sequence value in "Sequence" for the "Post" record, and returns this value.

    Finally, you just must to assign this value to your key field before savechanges.

    Hope this could be helpful for you.

    Regards,

    JA Reyes.

     


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Wednesday, March 13, 2013 3:50 PM