Skip Navigation LinksBase4.NET : Quickstarts : Schemas : Create a Type with an Autonumber key?

Skip Navigation Links.

Create an AutoNumber key?

Base4 supports auto numbers as the key for a type. Basically an auto number corresponds to an [int] IDENTITY database field. To declare an auto number you simply need to do this:

 

    <Type FullName="Base4.Core.Invoice" baseName="Base4.Storage.ItemImpl" keyProperty="Number" version="1">

      <Property name="Number" typeName="System.Int32" auto="True"/>
      <Property name="Description" typeName="System.String" length="100"/>
    </Type>

 

In this example we are saying that creating a Number field is an autonumber by setting the auto attribute to true. Base4 will automatically handles getting the database generated Number back to the Invoice after the Save(), so that it is known and can be used for updates. You can see how you might use this in the example below:

static void CreateAnInvoice()
{
    //Create an Invoice
    Invoice invoice = new Invoice();
    invoice.Description = "Interim paymnet for Project X";
    invoice.Save();
    Console.WriteLine("This invoice was given the Number# {0}",invoice.Number);
    //Correct the Description - Base4 knows which object to update.
    invoice.Description = "Interim payment for Project X";
    invoice.Save();

}