I have binding into the class, but not in another. I can`t call Product from Item: Object reference not set to an instance of an object.
Whats wrong?
How optimize this code?
foreach (var xd in excelData)
{
Product p = new Product {
Name = xd.ProductName,
};
ctx.Products.Add(p);
ctx.SaveChanges();
Item t = new Item {
Product=p,
};
t.ProductId = t.Product.ProductId;
ctx.Items.Add(t);
ctx.SaveChanges();
t.Product = ctx.Products.Where(c => c.ProductId == t.ProductId).FirstOrDefault();
ctx.SaveChanges();
}
ObjectQuery.Include Method. Specifies the related objects to include in the query results.