【问题标题】:insert data into two different tables using Linq使用 Linq 将数据插入到两个不同的表中
【发布时间】:2013-09-06 04:33:01
【问题描述】:

我正在使用 ASP .net 框架 4.5 MVC4

我有两个不同的表如下

public class Product
{
    public int ProductID { get; set; }

    public string Name { get; set; }
    public string Description { get; set; }

    public DateTime CreateTime { get; set; }
    public DateTime LastUpdateTime { get; set; }

    public virtual List<ProductVariants> ProductVariants { get; set; } 
}

public class ProductVariants
{
    public int ProductVariantsID { get; set; }
    public int ProductID { get; set; }

    public string FrontImage { get; set; }
    public string BackImage { get; set; }

    public string Color { get; set; }
}

我正在使用以下代码一次将数据提交到这些表中。

Product product = new Product
            {
                Name = model.Product.Name,

                Description = model.Product.Description,
                CreateTime = DateTime.Now,
                LastUpdateTime = DateTime.Now,
            };

            ProductVariants pv = new ProductVariants
            {
                FrontImage = model.FrontImage.FileName,
                BackImage = model.BackImage.FileName,
                Color = model.ProductVariants.Color,
            };

            product.ProductVariants.Add(pv);
            dbcontext.Products.Add(product);
            dbcontext.SaveChanges();

我做错了什么??我是否也必须提交 ProductVariants:

  dbcontext.ProductsVariants.Add(pv);

如果是,那么我将如何在 Product 中将 ProductVariants 值添加为虚拟 ..?

请帮忙!!!

【问题讨论】:

  • 您发布的代码有什么问题?你看到任何错误吗?实体之一是否未正确插入?
  • 我收到 System.NullReferenceException:对象引用未设置为对象的实例。在 product.ProductVariants.Add(pv);
  • 实际上我是 ASP.net 的新手,我很清楚代码应该如何准确地在多个表中插入数据..

标签: linq asp.net-mvc-4


【解决方案1】:

问题在于属性ProductVariants 未正确初始化。通常,您必须为其提供一个初始值(例如,在 Product 构造函数中)。

但是,您似乎正在使用实体框架和代码优先开发。如果确实如此,请不要将您的财产声明为List&lt;T&gt;,而是使用ICollection&lt;T&gt;

public class Product
{
    public int ProductID { get; set; }

    public string Name { get; set; }
    public string Description { get; set; }

    public DateTime CreateTime { get; set; }
    public DateTime LastUpdateTime { get; set; }

    public virtual ICollection<ProductVariants> ProductVariants { get; set; } 
}

这允许 Entity Framework 自动将属性与ICollection&lt;T&gt; 的适当实现(而不是基本的List&lt;T&gt;)关联起来。

另外,您可能需要使用Create 方法创建实体,而不是直接构造新实例:

Product product = dbcontext.Products.Create();
product.Name = model.Product.Name;
...

ProductVariants pv = dbcontext.ProductVariants.Create(); 
pv.FrontImage = model.FrontImage.FileName;
...

product.ProductVariants.Add(pv);
dbcontext.Products.Add(product);
dbcontext.SaveChanges();

【讨论】:

  • 是的,我正在使用 Code First Entity Frame Work。我只是尝试了 ICollection 而不是 List。但仍然遇到同样的异常。
  • @Inder 对不起,我忘了提到使用DbSet.Create 而不是直接构造实体。请参阅我的更新答案。
  • 非常感谢您的时间和精力。这是我如何解决的.. List pvlist = new List(); pvlist.Add(pv);产品.ProductVariants = pvlist; dbcontext.Products.Add(product); dbcontext.SaveChanges();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
相关资源
最近更新 更多