I'm working with three tables, but only inserting into two( one called images, and the other an intermediate table). They all have relationships.
Tables:
ProductGroups--
ID
Name
ProductGroup_Images--
ProductGroupID
ImagesID
Images-
ImageID
Path
Could the following code be written more elegantly?
using (StoreDataContext db = new StoreDataContext())
{
Image img = new Image
{
Path = "https://s3.amazonaws.com/mystore/images/public/" + FileUpload1.PostedFile.FileName,
};
db.Images.InsertOnSubmit(img);
db.SubmitChanges();
var pg = db.ProductGroups.Where(a => a.Name == txtName.Value).Select(b => b.ID).Single();
ProductGroups_Image xref = new ProductGroups_Image
{
ProductGroupsID = pg,
ImagesID= img.ImagesID
};
db.ProductGroups_Images.InsertOnSubmit(xref);
db.SubmitChanges();
}