I have the following database tables/EF objects
public class Transaction
{
//some other properties
public ICollection<TransactionItems> Items {get; set;}
}
public class TransactionItems
{
//some properties
}
What I need to do is, create a new instance of transaction along with several instances of TransactionItems for its Items property and save all of these to my DB
I have tried the following:
Transaction trans = new Transaction();
//set its properties
Then in a foreach loop I am looping through a collection and creating a new TransactionItem for each member and attempting to add it to the trans object Item Collection
foreach(var item in myCollection)
{
TransactionItem newItem = new TransactionItem();
//set its properties
//add it to the tran Item collection
tran.TransactionItems.Add(newItem);//getting null reference here...
}
I am getting a null reference exception when I attempt to add a transactionITem to the Item collection of my Transaction object. What am I doing wrong?