簡體   English   中英

在SaveChanges方法之前將獲得對新插入的Identity ID的訪問權限

[英]Getting access to newly inserted Identity ID before SaveChanges method will be called

我正在使用LINQ Entity Framework,並且遇到了以下場景:在使用過程執行多項操作之前,需要訪問新插入的Identity記錄。 以下是代碼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);**

                    newQuoteId = currentQuote.QuoteId; 
                }
            }
            else return;

            // Execution of some stored Procedure by using above newly generated QuoteId

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

在下一個函數quoteContainer.SaveChanges();中 將被調用以提交數據庫更改。

有人可以建議上述方法是否正確嗎?

到目前為止正確。
記住:你不能得到IDENTITY直到插入已發生! 更新時,您的實體已經擁有IDENTITY (主要是PK

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM