Is saving to database just to get an ID a bad hack?

Posted by Narsil on Stack Overflow See other posts from Stack Overflow or by Narsil
Published on 2010-04-24T05:15:12Z Indexed on 2010/04/24 5:23 UTC
Read the original article Hit count: 276

I hope the title is not too confusing. I am trying to make folders with linq-to-sql objects' IDs. Actually I have to create folders before I should save them. I will use them to keep user uploaded files. As you can see I have to create the folder with the FileID before I can save it there. So I just save a record which will be edited or maybe deleted

File newFile = new File();
...//add some values to fields so they don't throw rule violations
db.AddFile(newFile);
db.Save();
System.IO.Directory.CreateDirectory("..Uploads/"+newFile.FileId.ToString());

After that I will have to edit some fields and save again. Of course user might stop upload and I would have to delete it. I know I can write a stored procedure to get the next available FileID but some other upload happening at the same time would get the same number. So they would write in same directory which is a thing I don't want. Should I go on with this, would there be some problems? Can you think of a better way?

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about linq-to-entities