简体   繁体   English

LINQ to SQL事务和读取记录的ID

[英]LINQ to SQL transaction and read ID of inserted record

任何人都可以解释我如何在Linq中插入带有事务的数据库并读取该记录的id?

If you don't have a DBML setup, then add one to your project. 如果您没有DBML设置,请在项目中添加一个。
To add a DBML to your project from Solution Explorer: 要从解决方案资源管理器中将DBML添加到项目中:
Right click on your project -> go to Add new Item -> select LINQ to SQL Classes

To fill the DBML with your tables go to: 要使用表填充DBML,请转到:
View -> Server Explorer
Expand your database and drag your tables onto the dbml viewer 展开数据库并将表拖到dbml查看器上

Then if your table is caleld Widget: 然后如果你的表是caleld Widget:

DataClasses1DataContext db = new DataClasses1DataContext(myConnection);
db.Widgets.InsertOnSubmit(myWidget);

db.SubmitChanges();

//Here myWidget.Id will be set

Simply commit your changes to the database. 只需将更改提交到数据库即可。 The newly inserted entity will have it's ID property automatically updated with the newly assigned id and you can read it from there. 新插入的实体将使用新分配的ID自动更新其ID属性,您可以从那里读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM