编写LINQ查询的最佳方法是什么?该查询插入一条记录,然后使用C#返回新插入记录的主键?
主键值将在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;
任何AutoGeneratedValue为true的字段都将在dc将该记录提交到数据库后填写。SubmitChanges()