2010-05-20 34 views
3

如何在插入时从表格中获取自动生成的标识?如何在插入时从表格中获取自动生成的ID?

我有三个表

项目

ProjectXMilestone

里程碑

和里程碑的插入我也想插入第二个表中的行,所以我需要MilestoneId?凭什么我明白了? 我正在使用Linq。

+0

你用什么样的数据提供程序的? MSSQL? Mysql?你是否使用LinqToSql类来获取你的数据? – KroaX 2010-05-20 07:30:18

+0

Sql server 2005 – 2010-05-20 07:44:46

回答

3

检查该公司使用的LINQ to SQL

PractiseDataContext pDc = new PractiseDataContext(); 



// Here CustomerId is the Identity column(Primary key) in the 'CustomerDetails' table 

CustomerDetail cust = new CustomerDetail(); 

cust.CustomerName = "LINQ to SQL"; 



pDc.CustomerDetails.InsertOnSubmit(cust); 

pDc.SubmitChanges(); 



Response.Write(cust.CustomerId.ToString()); 
0

如果使用LinqToSql,你的DataContext从DB制成,你的数据库设置了关系,你不必在意这个的。如果Project与ProjectMilestones之间存在1:n关系,则您的LinqToSql类项目应该有一个ProjectMilestones集合,您只需填写。

你应该能够做这样的事情:

Project pr = new Project(); 
pr.Milestones = new List<ProjectMilestone>(); 

pr.Milestones.add(new ProjectMilestone()); 
pr.Milestones.add(new ProjectMilestone()); 

DataContext.InsertOnSubmit(pr); 
pr.SubmitChanges(); 
相关问题