How to retrieve identity ID when inserting a row in the db using linq?
A:
LINQ to SQL should automatically retrieve the identity of the inserted object, and update the field you mapped to the primary key accordingly - so long as the mapped PK property is marked as [Column(IsDbGenerated=true)]
.
Pavel Minaev
2009-08-20 20:55:42
gratz on the sql-server silver badge Marc
Remus Rusanu
2009-08-20 21:28:03
Thanks, Remus! You're not far away from it yourself :-)
marc_s
2009-08-21 05:04:05
A:
I'm using the .net Entity Framework and I've came across the similar case. Following is the code sinppet:
public void SaveQuote(Domain.Quote currentQuote)
{
try
{
int newQuoteId;
//Add quote and quoteline details to db
if (currentQuote != null)
{
using (QuoteContainer quoteContainer = new QuoteContainer())
{
**quoteContainer.AddToQuote(currentQuote);**
**quoteContainer.SaveChanges();**
newQuoteId = currentQuote.QuoteId;
}
}
else return;
// Execution of some stored Procedure by using above newly generated QuoteId
}
catch (Exception ex)
{
throw ex;
}
}
Can any one suggest whether the above approach is correct?
sandeept
2010-02-09 15:29:07