简体   繁体   English

通过实体框架将相关记录插入数据库

[英]Inserting Related Records into Database via Entity Framework

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? 我遇到的问题是product.ProductImages为null - 我希望能够这样做INSTEAD做多个db.TableName.Add / db.SaveChanges,因为如果我理解正确EF创建一个交易,如果有任何失败,你将不会插入没有产品图像的幻影产品记录 - 如果这是有道理的?

change your Product Model ? 改变你的产品型号?

private IList<ProductImage> productImages_;
public virtual IList<ProductImage> ProductImages {
   get {
     return productImages_ ?? (productImages_= new List<ProductImage>());
   }
   set { productImages_ = value;}
}

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实体之前将新的ProductImage实体显式添加到db.ProductImages实体集中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM