How can we get an autogenerated Id from the Table While Inserting?
I have three tables
Project
ProjectXMilestone
Milestone
and on Insert of Milestone I also want to insert a row in 2nd Table so I need MilestoneId?so how can I get that?开发者_如何学Python I am using Linq.
check this its used for 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());
If you use LinqToSql, your DataContext is made from DB and your DB is set up with relations, you dont have to care about this at all. If there is a 1:n relation from Project to ProjectMilestones, your LinqToSql Class Project should have a Collection of ProjectMilestones, that you just have to fill.
You should be able to do something like:
Project pr = new Project();
pr.Milestones = new List<ProjectMilestone>();
pr.Milestones.add(new ProjectMilestone());
pr.Milestones.add(new ProjectMilestone());
DataContext.InsertOnSubmit(pr);
pr.SubmitChanges();
精彩评论