编写插入记录然后使用C#返回新插入记录的主键的LINQ查询的最佳方法是什么?
主键值将在SubmitChanges()之后的该属性中.
MyTable record = new MyTable(); record.Name = "James Curran"; db.MyTable.InsertOnSubmit(record); db.SubmitChanges(); Console.WriteLine("record inserted as ID : {0}", record.Id);
// Create a new Order object. Order ord = new Order { OrderID = 12000, ShipCity = "Seattle", OrderDate = DateTime.Now // … }; // Add the new object to the Orders collection. db.Orders.InsertOnSubmit(ord); // Submit the change to the database. try { db.SubmitChanges(); } catch (Exception e) { Console.WriteLine(e); // Make some adjustments. // ... // Try again. db.SubmitChanges(); } return ord.OrderID;