Developer Guide
Inserting Records

    The Create operation inserts one or more Entity records to the database.

    //inserting a new Account
    Account account = new Account { Name="New test account" };
    try
    {
      Create(account);
    }
    catch(DatabaseException ex)
    {
       //Optionally: check the errors
       SystemInfo.Debug(ex.Message);
    
       //Or automatically update the ModelState, so show the validation errors to users. 
       //this would populate the error message in ViewMessages control.
       UpdateModelState(ex);
    
       //usually you want to return the results to the user at this point.
       return View(account);
    }

     

    Below is an example of inserting three Accounts:

    List<Account> accts = new List<Account>();
    for(int i=0;i<3;i++)
    {
       Account a = new Account { Name=Format("Global Media {0}",i), BillingCity="Toronto" };
       accts.Add(a);
    }
    
    try
    {
      Create(accts);
    }
    catch(DatabaseException ex)
    {
       //Optionally: check the errors
       SystemInfo.Debug(ex.Message);
    
       //Or automatically update the ModelState, so show the validation errors to users.
       UpdateModelState(ex);
       
      //usually you want to return the results to the user at this point.
      return View(account);
    }

     

    Note: These errors are not automatically bound to field and ViewMessages components. Call UpdateModelState() method so that user can take corrective action if necessary.

     

    Processing in System Mode

    ​Magentrix for Create operation validates the user's access and verifies that user has the "create" permission. In some scenarios the database operation should take place regardless of the user's permission; in such cases use the SystemMode flag.

    Example of using the Create method in system mode:

    Account acc = new Account { Name="Sample Account" };
    
    //when called in system mode, the system level permission is checked rather than the user's  
    Create(acc, new { SystemMode=true });
    
     

    Transactional Commit

    In Magentrix database operations, each item is committed to the database as a separate transaction. If you wish to ensure that when one item fails to be inserted, the remainder of the records are not processed as well, you need to use the options parameter and set AllOrNone flag. The options parameter is optional. The example below shows how you can set the AllOrNone flag:
     
    try
    {
       Create(contacts, new { AllOrNone=true });
    }
    catch(DatabaseException ex)
    {
       //handle the errors
    }
    


    See Database Options for more information.