title: How do I get an identity value with Entity Framework(v5) before I save the record
body: <p>I am new to entity framework and I have been searching a while for an answer to this question and I can't find anything that directally addresses this. </p>
  
  <p>Here is the problem.  I have a table in Oracle.  In this table there are 2 fields(there are more but not important to this question).  Card_Transaction_Id and Parent_Card_Transaction_ID.  The Parent_Card_Transaction_Id field is constrained by the Card_Transaction_Id field and I am using a Oracle sequence via a trigger to populate the Card_Transaction_Id field.  </p>
  
  <p>In my code, I am using Entity Framework(Version 5) to connect using the Code First Approach. </p>
  
  <p>The issue is when I try to create a new record.  I need to know what the next sequence value is in order to populate the Parent_Card_Transaction_Id.  My mapping for card transactions:</p>
  
  <pre><code>public class CardTransactionMap : EntityTypeConfiguration&lt;CardTransaction&gt;
  {
      public CardTransactionMap(string schema)
      {
  
          ToTable("CARD_TRANSACTION", schema);
  
          // Mappings &amp; Properties
  
          // Primary Key
          HasKey(t =&gt; t.CardTransactionId);
          Property(t =&gt; t.CardTransactionId)
              .HasColumnName("CARD_TRANSACTION_ID")
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
          Property(t =&gt; t.ParentCardTransactionId)
              .HasColumnName("PARENT_CARD_TRANSACTION_ID");
  
          Property(t =&gt; t.CardProfileId)
              .HasColumnName("CARD_PROFILE_ID");
      }
  }
  </code></pre>
  
  <p><strong>The question is - is there any way to get the next sequence number before I save the record?</strong>  </p>
  
  <p>My current work arround is to use the following method:</p>
  
  <pre><code>    public static decimal GetNextCardTransactionSequenceValue()
      {
          using (var context = new Context(new OracleConnectionFactory().GetConnection()))
          {
  
              var sequence = context.Database.SqlQuery&lt;int&gt;("select card_transaction_id from card_transaction").ToList();
  
              return sequence[0];
  
          }   
  
      }
  </code></pre>
  
  <p>Using that method, I get the next value and then just populate my table. This works but I don't like doing it this way.  I feel that there must be a better way to do it.  </p>
  
  <p>Thanks in advance. </p>