开发者

How can we get an autogenerated Id from the Table While Inserting?

开发者 https://www.devze.com 2022-12-31 15:13 出处:网络
How can we get an autogenerated Id from the Table While Inserting? I have three tables Project ProjectXMilestone

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号