I have the following:
if (chemexist == false) // conditionally creating WtrChem record
{
WtrChem wc = new WtrChem();
wc.Contact = "John Smith";
..
wc.Phone = ("800-888-9988");
Db.WtrChem.Add(wc);
Db.SaveChanges();
}
WtrChemDetail psw = new WtrChemDetail (); // creating details record for WtrChem
psw.Comments = comment;
..
..
Db.WtrChemDetail.Add(psw);
Db.SaveChanges();
The code will first create the master record and then create a detail record. What I have works. I am wondering if there is a more efficient way to doing what I have above in terms of best practices.
You probably want to model this a bit different where the detail is a property on the master.
Something like this perhaps:
If you use this approach, the reference between the master and the detail will be sorted automatically.
You need to add a using statement in order to make the
.Include(..)lambda to work: