LinqConnect Documentation
In This Topic
    Modifying And Saving Entities
    In This Topic
    Modifying and Saving Entities
    LinqConnect Documentation
    Modifying and Saving Entities
    [email protected]

    In this article we use a model created with the help of Entity Developer (read more about it in the section Using Entity Developer) or created manually (read more about it in the section Creating LinqConnect Classes Manually).

    LinqConnect is designed to offer maximum flexibility in manipulating and persisting changes made to your objects. As soon as entity objects are available – either by retrieving them through a query or constructing them anew – you may manipulate them as normal objects in your application, changing their values or adding and removing them from collections as you see fit. LinqConnect tracks all your changes and is ready to transmit them back to the database as soon as you are done.


    CautionNote:

    To run samples from this article include the following namespace before your code:

    using CrmDemoContext;
    
    
    Imports CrmDemoContext
    
    


    In this sample, we insert a newly created company into the database:
    CrmDemoDataContext db = new CrmDemoDataContext();
    
    // Create a new Company object
    Company comp = new Company();
    
    // Set the company properties
    comp.CompanyID = 20;
    comp.Address = "Company Address";
    comp.City = "Some city";
    comp.CompanyName = "New company";
    
    // Insert the company to the corresponding table.
    // Note that no changes are made in the database until the SubmitChanges method is called.
    db.Companies.InsertOnSubmit(comp);
    
    // Ask DataContext to save all changes to the database.
    db.SubmitChanges();
    
    
    Dim db = New CrmDemoDataContext()
    
    ' Create a new Company object
    Dim comp = New Company()
    
    ' Set the company properties
    comp.CompanyID = 20
    comp.Address = "Company Address"
    comp.City = "Some city"
    comp.CompanyName = "New company"
    
    ' Insert the company to the corresponding table.
    ' Note that no changes are made in the database until the SubmitChanges method is called.
    db.Companies.InsertOnSubmit(comp)
    
    ' Ask DataContext to save all changes to the database.
    db.SubmitChanges()
    
    

    In the following example the specific company is updated:

    
    CrmDemoDataContext db = new CrmDemoDataContext();
    
    // Select a specific company. For example, select the one with id specified.
    int id = 20;
    Company comp = db.Companies.Single(c => c.CompanyID == id);
    
    // Change the company address.
    // Note that no changes are made in the database until the SubmitChanges method is called.
    comp.Address = "New Address";
    
    // Ask DataContext to save the changes.
    db.SubmitChanges();
    
    
    Dim db = New CrmDemoDataContext()
    
    ' Select a specific company. For example, select the one with id specified.
    Dim id As Integer = 20
    Dim targetCompany = db.Companies.Single(Function(c) c.CompanyID = id)
    
    ' Change the company address.
    ' Note that no changes are made in the database until the SubmitChanges method is called.
    targetCompany.Address = "New Address"
    
    ' Ask DataContext to save the changes.
    db.SubmitChanges()
    
    

    In the following example we delete a company with the id column specified:

    CrmDemoDataContext db = new CrmDemoDataContext();
    
    // Select a specific company.
    int id = 20;
    Company comp = db.Companies.Single(c => c.CompanyID == id);
    
    // Delete the selected company.
    // Note that no changes are made in the database until the SubmitChanges method is called.
    db.Companies.DeleteOnSubmit(comp);
    
    // Ask DataContext to save the changes.
    db.SubmitChanges();
    
    
    Dim db = New CrmDemoDataContext()
    
    ' Select a specific company.
    Dim id As Integer = 20
    Dim targetCompany = db.Companies.Single(Function(c) c.CompanyID = id)
    
    ' Delete the selected company.
    ' Note that no changes are made in the database until the SubmitChanges method is called.
    db.Companies.DeleteOnSubmit(targetCompany)
    
    ' Ask DataContext to save the changes.
    db.SubmitChanges()
    
    

    To know more about editing data, visit the Modifying Data section.