0

Right now I am working on a simple website in ASP.NET MVC 4 using Entity Framework and want to be able to upload photos in the Create view. I have a table for storing data about the photos, including a ProductID.

What is the way of assigning a ProductID to the uploaded photos, when, at the time of the photo uploading, the product hasn't been created yet and thus I don't know it's ID?

Thank you!

EDIT: Just to clarify - I am saving the stuff about the photos as soon as they are uploaded, then I save the product and then I need to add the ProductID to each of the photos.

jkottnauer
  • 403
  • 9
  • 18
  • Are you using an ORM (such as Entity Framework, nHibernate etc...)? What database are you using? How are you accessing the database? – Oded Dec 05 '12 at 13:00

2 Answers2

1

Assuming you are using Entity Framework (you didn't mention), here is some pseudo code:

Product product = new Product();
// product.Name = ""; etc.
db.Products.Add(product);
db.SaveChanges();
Photo.ProductId = product.Id;

After the product is saved to the database, it has an ID which you can use for the Photo.

user1797792
  • 1,169
  • 10
  • 26
  • You are assuming the OP is using entity framework, though this is not mentioned _anywhere_ in the question. – Oded Dec 05 '12 at 12:03
  • He's also not denying it. Since he is using MVC4, chances are high that he is using EF. I just assumed it – user1797792 Dec 05 '12 at 12:04
  • And yet, this is quite an assumption. – Oded Dec 05 '12 at 12:10
  • Assuming he is not using MS SQL and no framework at all, is too quite an assumption... I don't see your problem – user1797792 Dec 05 '12 at 12:13
  • True, but my answer contains these assumptions. The OP can comment on those. With this answer, unless the OP _knows_ this is EF, the only things he can say is "it doesn't work" or "What is SaveChanges?". If you make assumptions, document them. – Oded Dec 05 '12 at 12:15
  • Ok, thank you. That's some good feedback. I'm quite new to answering questions on SO, so excuse me for not knowing that. I will edit my answer – user1797792 Dec 05 '12 at 12:18
  • Thanks for the answer. I am using Entity Framework, (sorry for not mentioning it, will edit my post). However, my problem is that the images are uploaded using Valums Uploader as soon as they are selected and so are saved in the database before the product. I basically need to save the photos, then the product and then assign the product ID to each of the photos. – jkottnauer Dec 05 '12 at 12:34
0

What is the way of assigning a ProductID to the uploaded photos, when, at the time of the photo uploading, the product hasn't been created yet and thus I don't know it's ID?

The standard way of doing this in SQL Server is to make the ID field an IDENTITY field - that will automatically increment as you insert rows. You can return the new id from the database using SCOPE_IDENTITY.

Other relational databases have similar schemes.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009