0

this below way i am inserting customer and nested child. customer has child called address and address has child called contacts details.

    using (var db = new TestDBContext())
    {
        var customer = new Customer
        {
            FirstName = "Test Customer1",
            LastName = "Test Customer1",
            Addresses = new List<Addresses>
            {
                new Addresses
                {
                    Address1 = "test add1",
                    Address2 = "test add2",
                    IsDefault=true,
                    Contacts =  new List<Contacts>
                    {
                       new Contacts {  Phone = "1111111", Fax = "1-1111111",IsDefault=true, SerialNo=1 },
                       new Contacts {  Phone = "2222222", Fax = "1-2222222",IsDefault=false, SerialNo=2  }
                    }
                },
                new Addresses
                {
                    Address1 = "test add3",
                    Address2 = "test add3",
                    IsDefault=false,
                    Contacts =  new List<Contacts>
                    {
                       new Contacts {  Phone = "33333333", Fax = "1-33333333",IsDefault=false, SerialNo=1 },
                       new Contacts {  Phone = "33333333", Fax = "1-33333333",IsDefault=true, SerialNo=2  }
                    }
                }

            }
        };

        db.Customer.Add(customer);
        db.SaveChanges();

        int id = customer.CustomerID;
    }

suppose now i want to update customer and its associated address and contacts details.

i browser few similar thread here. i saw people deleting child data and insert new one instead of update. here is one link https://stackoverflow.com/a/27177623/728750

they include child this way

var existingParent = _dbContext.Parents
        .Where(p => p.Id == model.Id)
        .Include(p => p.Children)
        .SingleOrDefault();

but in my case i have multiple child say like address and contact details then how could i Include first address of customer and then i like to include contact details child of address.

please tell me how to do it.

1 Answer 1

1

Are you looking for this

using System.Data.Entity; // NB!

var company = dbContext.Parents
                     .Include(co => co.Addresses.Select(ad=> ad.Contacts))
                     .FirstOrDefault(p =>  p.Id == model.Id);

Brief sample

var company = context.Companies
                 .Include(co => co.Employees.Select(emp => emp.Employee_Car))
                 .Include(co => co.Employees.Select(emp => emp.Employee_Country))
                 .FirstOrDefault(co => co.companyID == companyID);

.Include Msdn details summarized

To include a collection, a collection, and a reference two levels down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Collection.Select(l2 => l2.Level3Reference)))

Sign up to request clarification or add additional context in comments.

3 Comments

yes it worked. suppose if we have many multiple nested entity then how can i do it. can post a sample code where nested entity is say 5.
Glad, it helped you
i want nested like company > country > state > city how to achieve this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.