Ninja Database Pro
Save<T>(T) Method
Example 



KellermanSoftware.NinjaDatabasePro Namespace > NinjaDbPro Class > Save Method : Save<T>(T) Method
The entire object graph will be saved to the database in an implicit transaction. If there is a problem saving any object, the implicit transation will be automatically rolled back. To save child or parents seperately, define relationships as atributes. The object must have a primary key property of Id or ObjectNameId as a type of long or an exception will be thrown. Example primary keys: Id or CustomerId If the primary is zero, the object will be inserted and set when it is saved. If the primary key is greater than zero the object will be updated. CreateDate, if defined will be filled in with the DateTime.Now during an insert UpdateDate, if defined will be filled in with the DateTime.Now during an update.
Syntax
Public Overloads Function Save(Of T As {Class, New})( _
   ByVal value As T _
) As T
Dim instance As NinjaDbPro
Dim value As T
Dim value As T
 
value = instance.Save(Of T)(value)
public T Save<T>( 
   T value
)
where T: class, new()
public:
T^ Savegeneric<typename T>
( 
   T^ value
) 
where T: ref class, gcnew()

Parameters

value

Type Parameters

T

Return Value

The object populated with the primary key.
Example
NinjaDbPro db = new NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName");
             
            //Licensed Mode
            //db.UserName = "John Smith 101224";
            //db.LicenseKey = "aousdf832jasf==";
             
            //Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
            //db.Storage = new MemoryDatabase(); //In memory database
            //db.Storage = new FileDatabase();  
             
            Order order = new Order();
            order.Buyer = new Person();
            order.Buyer.Name = "John Doe";
            order.Buyer.DateCreated = DateTime.Now;
            order.OrderDetails = new List<OrderDetail>();
             
            OrderDetail detail1 = new OrderDetail();
            detail1.Item = new Product();
            detail1.Item.Name = "Wizard";
            detail1.Item.Cost = 29.98M;
            detail1.Quantity = 2;
            detail1.Order = order;
            order.OrderDetails.Add(detail1);
             
            OrderDetail detail2 = new OrderDetail();
            detail2.Item = new Product();
            detail2.Item.Name = ".NET Caching Library";
            detail2.Item.Cost = 199.95M;
            detail2.Quantity = 1;
            detail2.Order = order;
            order.OrderDetails.Add(detail2);
             
            db.OpenDatabase();
             
            //Everything will be saved to the database.
            //Child and parent objects can be saved seperately by setting up relationships as attributes.
            //This is wrapped in an implicit transaction, if any record fails it will all roll back
            db.Save(order);
             
            //Reload the entire tree
            Order orderCopy = db.Load<Order>(order.OrderId);
             
            //Get just a single object
            Person personCopy = db.Load<Person>(order.Buyer.PersonId);
             
            db.CloseDatabase();
Dim db As New NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName")
             
            'Licensed Mode
            'db.UserName = "John Smith 101224";
            'db.LicenseKey = "aousdf832jasf==";
             
            'Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
            'db.Storage = new MemoryDatabase(); //In memory database
            'db.Storage = new FileDatabase();  
             
            Dim order As New Order()
            order.Buyer = New Person()
            order.Buyer.Name = "John Doe"
            order.Buyer.DateCreated = Date.Now
            order.OrderDetails = New List(Of OrderDetail)()
             
            Dim detail1 As New OrderDetail()
            detail1.Item = New Product()
            detail1.Item.Name = "Wizard"
            detail1.Item.Cost = 29.98D
            detail1.Quantity = 2
            detail1.Order = order
            order.OrderDetails.Add(detail1)
             
            Dim detail2 As New OrderDetail()
            detail2.Item = New Product()
            detail2.Item.Name = ".NET Caching Library"
            detail2.Item.Cost = 199.95D
            detail2.Quantity = 1
            detail2.Order = order
            order.OrderDetails.Add(detail2)
             
            db.OpenDatabase()
             
            'Everything will be saved to the database.
            'Child and parent objects can be saved seperately by setting up relationships as attributes.
            'This is wrapped in an implicit transaction, if any record fails it will all roll back
            db.Save(order)
             
            'Reload the entire tree
            Dim orderCopy As Order = db.Load(Of Order)(order.OrderId)
             
            'Get just a single object
            Dim personCopy As Person = db.Load(Of Person)(order.Buyer.PersonId)
             
            db.CloseDatabase()
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

NinjaDbPro Class
NinjaDbPro Members
Overload List