title: Using StoreGeneratedPattern.Identity with database-trigger-generated primarykey values not possible?
body: <p>This question is related to another question i asked here ( <a href="https://stackoverflow.com/questions/8510939/entity-framework-4-2-how-to-realize-tpt-inheritance-with-database-generated-pr">Entity Framework 4.2 - How to realize TPT-Inheritance with Database-generated Primarykey Value?</a> ) and should simply clarify, if my assumptions, regarding the problem stated in the topic, are right or not.</p>
  
  <p><strong>The Problem (in detail):</strong></p>
  
  <ul>
  <li>I want to use EF (4.1) to access a database, that already exists</li>
  <li>the database has some restrictions regarding the generation of primary key values for its tables (there is a UDF that takes a tablename and returns the next available ID)</li>
  <li>To make things as easy as possible for myself, my first approach was to define database triggers (before insert) that call the ID-generating UDF to set the new ID on inserting a new datarow</li>
  <li>Then i set the StoreGeneratedPattern properties of the corresponding entities in the csdl of my EDM to "Identity", so that the newly generated IDs would be set in the entity objects after saving them to the DB</li>
  </ul>
  
  <p><strong>The Result of this was:</strong></p>
  
  <p>When I created a new entity object, added it to the DbContext and called SaveChanges on it, the corresponding datarow was inserted in the database, but the entity was not updated with the new database-generated ID. I realized this when i tried to save more at once, that have associations to each other (parent-child), because the foreignkey properties of the child entities could not be set correctly, since the new ID of the parent was not known to the DbContext.</p>
  
  <p>This is the reason I asked the above mentioned question concerning TPT inheritance.</p>
  
  <p>After several days of research and trying everything that came to my mind to solve this problem, i think i realized, that this simply cannot work. Although the documentation of the StoreGeneratedPattern enum at MSDN and several explanations in blogs suggests, that StoreGeneratedPattern.Identity should be set to retrieve the generated value, when the DB generates a value on inserting a new row, this is not true for primary keys in conjunction with database triggers.</p>
  
  <p>After thinking about that a long time, that seems perfectly logical to me, since the EF needs some criterium to retrieve database-generated values, and i think that would be in most cases the identity of an entity. For databasecolumns that are set to autoincrement (or identity-column, ...) that might be no problem, because the DBMS provides some functionality to retrieve the last inserted identity-value (e.g. @@identity in MSSQL). But when using a trigger to generate a new identity-value, the EF obviously doesnt know how to query the newly inserted row (and i cant imagine any good db-independent way to do this either).</p>
  
  <p>So my actual question is: are the assumptions above correct or am I overlooking something important here?</p>
  
  <p>Thanks in advance for any clarification/inspiration on this.</p>
  
  <p><strong>Edit (followup question):</strong></p>
  
  <p>After reading the answer from Ladislav another question arises:</p>
  
  <p>If I set StoreGeneratedPattern in CSDL, do I have to set it to the same value in SSDL (and vice versa)? The patch for the edm designer implies that this is the case, because it automatically synchronizes the StoreGeneratedPattern in SSDL when you change it in CSDL (through the designer).</p>