Inserting Related Records into Database via Entity Framework

z0id

New Member
I'm curious if there is a clean way to do this\[code\]Product product = new Product();product.CreateDateTime = DateTime.Now;product.Description = productCreateModel.Product.Description;product.ManufacturerId = productCreateModel.Manufacturer;product.MetaDescription = productCreateModel.Product.MetaDescription;product.MetaTitle = productCreateModel.Product.MetaTitle;product.Name = productCreateModel.Product.Name;product.Status = ProductStatuses.Active;product.URL = productCreateModel.Product.URL;if (productCreateModel.ProductImage1.ContentLength > 0) { BinaryReader binaryReader = new BinaryReader(productCreateModel.ProductImage1.InputStream); product.ProductImages.Add(new ProductImage() { CreateDateTime = DateTime.Now, Image = binaryReader.ReadBytes(productCreateModel.ProductImage1.ContentLength), PrimaryImage = true }); }db.Products.Add(product);db.SaveChanges();\[/code\]The problem i'm running into is that the product.ProductImages is null - I'd love to be able to do it this way INSTEAD of doing multiple db.TableName.Add/db.SaveChanges because if I understand it correctly EF creates a transaction so that if anything fails you won't have phantom product records inserted with no product images - if that makes sense?
 
Back
Top