I am using LINQ-to-SQL class. It has a method object.InsertOnSubmit() .But it returns void so can i get primary key values after inserting a record.
我正在使用LINQ-to-SQL类。它有一个方法object.InsertOnSubmit()。但它返回void,所以我可以在插入记录后获得主键值。
I want the primary key value of recently inserted record.
我想要最近插入的记录的主键值。
6 个解决方案
#1
yes (Assuming it is an identity field).
是(假设它是一个身份字段)。
Calling InsertOnSubmit alone doesn't send the insert to the db. You need to call SubmitChanges in order for any changes in the current datacontext instance to be sent to the db.
单独调用InsertOnSubmit不会将插入发送到db。您需要调用SubmitChanges以便将当前datacontext实例中的任何更改发送到db。
After you have called SubmitChanges, you can access the values from the instance you used when calling InsertOnSubmit. Linq to sql will populate those values for you when doing the insert (which occurs during SubmitChanges)
调用SubmitChanges后,可以从调用InsertOnSubmit时使用的实例访问值。 Linq to sql将在执行插入时为您填充这些值(在SubmitChanges期间发生)
Check this example: http://msdn.microsoft.com/en-us/vbasic/bb737920.aspx#dynid
请查看此示例:http://msdn.microsoft.com/en-us/vbasic/bb737920.aspx#dynid
#2
In short, you don't need to. The object itself is updated.
简而言之,你不需要。对象本身已更新。
public void Add(Person person)
{
using (MyEntities context = new MyEntities())
{
Context.Persons.InsertOnSaveChanges(person);
Context.SaveChanges();
}
}
public void Foo()
{
Person p = new Person { name = "John", age = 20 }
Add(p);
Int32 id = p.id; // id contains the newly inserted object's id
}
#3
Here's an example of how that works:
这是一个如何工作的例子:
var dc = new MyDataContext();
var cust = new Customer{FirstName="John", LastName="Smith"};
dc.Customer.InsertOnSubmit(cust);
dc.SubmitChanges();
//after SubmitChanges(), the Id is filled from the database
var customerPrimaryKey = cust.Id;
#4
If the model is set up properly, the PK should be set on the affected object (the one you just inserted) automagically.
如果模型设置正确,则应自动在受影响的对象(刚刚插入的对象)上设置PK。
HTH.
Seth
#5
Once you have called the InserOnSubmit(),primary key value is set to the corrosponding fiels in youe table object. You can simply access that property to get Primary Key.
调用InserOnSubmit()后,主键值将设置为youe表对象中的corrosponding fiels。您只需访问该属性即可获得主键。
#6
Are you calling SubmitChanges() on your data context after inserting the record? The changeset won't be evaluated until you do.
在插入记录后,您是否在数据上下文中调用SubmitChanges()?在您这样做之前,不会评估变更集。
#1
yes (Assuming it is an identity field).
是(假设它是一个身份字段)。
Calling InsertOnSubmit alone doesn't send the insert to the db. You need to call SubmitChanges in order for any changes in the current datacontext instance to be sent to the db.
单独调用InsertOnSubmit不会将插入发送到db。您需要调用SubmitChanges以便将当前datacontext实例中的任何更改发送到db。
After you have called SubmitChanges, you can access the values from the instance you used when calling InsertOnSubmit. Linq to sql will populate those values for you when doing the insert (which occurs during SubmitChanges)
调用SubmitChanges后,可以从调用InsertOnSubmit时使用的实例访问值。 Linq to sql将在执行插入时为您填充这些值(在SubmitChanges期间发生)
Check this example: http://msdn.microsoft.com/en-us/vbasic/bb737920.aspx#dynid
请查看此示例:http://msdn.microsoft.com/en-us/vbasic/bb737920.aspx#dynid
#2
In short, you don't need to. The object itself is updated.
简而言之,你不需要。对象本身已更新。
public void Add(Person person)
{
using (MyEntities context = new MyEntities())
{
Context.Persons.InsertOnSaveChanges(person);
Context.SaveChanges();
}
}
public void Foo()
{
Person p = new Person { name = "John", age = 20 }
Add(p);
Int32 id = p.id; // id contains the newly inserted object's id
}
#3
Here's an example of how that works:
这是一个如何工作的例子:
var dc = new MyDataContext();
var cust = new Customer{FirstName="John", LastName="Smith"};
dc.Customer.InsertOnSubmit(cust);
dc.SubmitChanges();
//after SubmitChanges(), the Id is filled from the database
var customerPrimaryKey = cust.Id;
#4
If the model is set up properly, the PK should be set on the affected object (the one you just inserted) automagically.
如果模型设置正确,则应自动在受影响的对象(刚刚插入的对象)上设置PK。
HTH.
Seth
#5
Once you have called the InserOnSubmit(),primary key value is set to the corrosponding fiels in youe table object. You can simply access that property to get Primary Key.
调用InserOnSubmit()后,主键值将设置为youe表对象中的corrosponding fiels。您只需访问该属性即可获得主键。
#6
Are you calling SubmitChanges() on your data context after inserting the record? The changeset won't be evaluated until you do.
在插入记录后,您是否在数据上下文中调用SubmitChanges()?在您这样做之前,不会评估变更集。