4

I'm curious if there is a clean way to do this

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();

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?

1
  • I'm pretty sure you can do it with only a single db.SaveChanges(), no matter how many objects you've added or what their relationships are. Commented Oct 5, 2012 at 15:06

2 Answers 2

4

change your Product Model ?

private IList<ProductImage> productImages_;
public virtual IList<ProductImage> ProductImages {
   get {
     return productImages_ ?? (productImages_= new List<ProductImage>());
   }
   set { productImages_ = value;}
}
Sign up to request clarification or add additional context in comments.

2 Comments

this worked perfectly - I didn't think it would work as I was using Code First and wasn't sure if .net would understand this and still relate it properly to my database - however this was perfect - thanks
@Loren well, the simple public virtual MyList {get;set;} is often presented in examples, but this way of doing should be preferred, when you need to instanciated the model (and having the "Navigation lists" instanciated).
0

I'm just brain storming here so don't get upset if this doesn't work, but I think you might need to explicitly add the new ProductImage entity to the db.ProductImages entity set before you link it to the Product entity.

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;

db.Products.Add(product);

if (productCreateModel.ProductImage1.ContentLength > 0)
    {
        BinaryReader binaryReader = new BinaryReader(productCreateModel.ProductImage1.InputStream);

        ProductImage image = new ProductImage()
        {
              CreateDateTime = DateTime.Now,
              Image = binaryReader.ReadBytes(productCreateModel.ProductImage1.ContentLength),
              PrimaryImage = true
        }

        db.ProductImages.Add(image); // Add the image to the ProductImage entity set
        product.ProductImages.Add(image); // link the image to this Product
    }

db.SaveChanges(); // Save all changes

3 Comments

hmmm, the problem is not there, it's just that product.ProductImages (the "navigation list" from the model Product) is not instanciated... And it's hard to add an item to a null List ;)
Then you must be getting a NullReferenceException when you call db.ProductImages.Add(), correct?
Yea correct - the thing about this though is I know i can do it this way I was just trying to keep it cleaner by not having to do all the db.tablename.adds - know what i mean? thanks for the try tho! Raphael's solution did the trick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.