LinqConnect Documentation
In This Topic
    Saving Related Entities
    In This Topic
    Saving Related Entities
    LinqConnect Documentation
    Saving Related Entities
    [email protected]

    When an entity object is marked for persisting (i.e., when the InsertOnSubmit method is called), the same is done for all the related entities. This is because the data context checks all navigation properties of this entity to find out whether any of them point to some other entity objects not being persisted yet.

    This means that it is sufficient to call InsertOnSubmit on only a single object, and the whole graph of related entities will be persisted on SubmitChanges as well. For example, we insert a ProductCategory object and related Products:


    CrmDemoDataContext context =
        new CrmDemoDataContext() { Log = Console.Out };
     
     
    Productcategory newCategory = new Productcategory()
    {
        CategoryID = 13,
        CategoryName = "Atlas",
        ParentCategory = 9
    };
     
    Product newProduct1 = new Product()
    {
        ProductID = 9001,
        ProductName = "North America maps",
        Productcategory = newCategory,
        InStock = 100,
        Price = 70,
        Discontinued = false
    };
     
    Product newProduct2 = new Product()
    {
        ProductID = 9002,
        ProductName = "Atlas of the World",
        Productcategory = newCategory,
        InStock = 100,
        Price = 99,
        Discontinued = false
    };
     
    context.Productcategories.InsertOnSubmit(newCategory);
    context.SubmitChanges();
    Dim context As New CrmDemoDataContext() With { _
        .Log = Console.Out _
    }
    Dim newCategory As New Productcategory() With { _
        .CategoryID = 13, _
        .CategoryName = "Atlas", _
        .ParentCategory = 9 _
    }
     
    Dim newProduct1 As New Product() With { _
        .ProductID = 9001, _
        .ProductName = "North America maps", _
        .Productcategory = newCategory, _
        .InStock = 100, _
        .Price = 70, _
        .Discontinued = False _
    }
     
    Dim newProduct2 As New Product() With { _
        .ProductID = 9002, _
        .ProductName = "Atlas of the World", _
        .Productcategory = newCategory, _
        .InStock = 100, _
        .Price = 99, _
        .Discontinued = False _
    }
     
    context.Productcategories.InsertOnSubmit(newCategory)
    context.SubmitChanges()
    

    As we can see from the log, all three objects are saved to the database at the SubmitChanges call. Actually, it does not matter that we insert the topmost node of the graph (as it was said, LinqConnect checks all navigation properties of the entity being inserted, whether they point to the 'master' or 'dependent' entities). To check this, you can insert one of the product instead of the category in the above sample, and both the category and the other product will be saved as well:


    ...
    context.Products.InsertOnSubmit(newProduct1);
    context.SubmitChanges();
    ...
    context.Products.InsertOnSubmit(newProduct1)
    context.SubmitChanges()
    

    The InsertAllOnSubmit method does the same as InsertOnSubmit with the only difference that it takes a collection of entity objects instead of a single one.