Link to object identification before submitting changes to LINQ

there is a way to know the identifier column identifier of the record inserted through InsertOnSubmitin advance, for example. before calling datasource SubmitChanges?

Imagine that I am filling in some kind of hierarchy in the database, but I would not want to send changes for each recursive call of each child element of the node (for example, if I had a directory table and a file table and I recreated my file system structure in the database )

I would like to do it this way, so I create a Directory object, set its name and attributes, then InsertOnSubmitto the DataContext.Directories collection, and then refer to Directory.ID in its child files. Currently, I need to call InsertOnSubmit to insert the "directory" into the database, and the database mapping fills its identifier column. But this creates a lot of transactions and database access, and I believe that if I did this in a batch package, performance would be better.

What I would like to do is somehow use Directory.ID before making the changes, create all my File and Directory objects in advance, and then make a large file that puts all the data in the database. I am also open to solving this problem using a stored procedure; I assume that performance will be even better if all operations are performed directly in the database.

+3
source share
2 answers

One way around this is to not use the identifier column. Instead, create an IdService that you can use in your code to get a new identifier each time a Directory object is created.

IdService, , . , . , Directory, , . , , , , , , 1000 (). . 1000 , 1000 . , bigint, .

Directory , , Files, .

. , . .

Linq to SQL.

+3

, SQL- LINQ, .

USE [database];
GO
DBCC CHECKIDENT ("schema.table", NORESEED);
GO
+1

Source: https://habr.com/ru/post/1725183/


All Articles