獲取剛剛插入數(shù)據(jù)庫的對(duì)象列表的最佳方法是什么?我有將新對(duì)象列表添加到數(shù)據(jù)庫的代碼,我想檢索對(duì)象列表而無需從數(shù)據(jù)庫中再次選擇。我希望insert在 SQL Server 觸發(fā)器中出現(xiàn)類似的東西。這是我的代碼片段。 List<string> productNames = new List<string>(); foreach (var name in productNames) { var product = new Product {Name = name, Color = "something", Body="something body" }; DbContext.Products.Add(product); } DbContext.SaveChanges(); var result = ListProductsInserted; // I want to get list product我不想重寫查詢以再次從數(shù)據(jù)庫中檢索數(shù)據(jù)。有沒有更好的辦法?
2 回答
瀟瀟雨雨
TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊
如果我正確理解您的問題,請(qǐng)使用下面的代碼
List<string> productNames = new List<string>();
List<Product> holdProducts = new List<Product>();
foreach (var name in productNames)
{
var product = new Product {Name = name, Color = "something", Body="something body" };
DbContext.Products.Add(product);
holdProducts.Add(product);
}
DbContext.SaveChanges();
var result = holdProducts;
海綿寶寶撒
TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
- 2 回答
- 0 關(guān)注
- 232 瀏覽
添加回答
舉報(bào)
0/150
提交
取消
