public void InsertToDifferentTable() { // Create the person var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 }; // Tell PetaPoco to insert it, but to the "SpecificPeople" table and not the "People" table. // Note: the id will only be returned if PetaPoco can tell which is the primary key via mapping or convention. var id = DB.Insert("SpecificPeople", person); // Obviously the ID returned will be the same at the one we set id.ShouldBe(person.Id); // Get a clone/copy from "People" table (Default table as per mappings) var clone = DB.SingleOrDefault<Person>(id); // As expected, doesn't exist clone.ShouldBeNull(); // We need to get the clone/copy from the correct table // Note: we can't use auto select builder here because PetaPoco would create columns such as People.Id clone = DB.Query<Person>("SELECT * FROM [SpecificPeople] sp WHERE sp.[Id] = @0", id).Single(); // See, they're are the same clone.ShouldBe(person); // But, they're not not reference equals as PetaPoco doesn't cache because it's a MircoORM. person.Equals(clone).ShouldBeFalse(); }
public void UpdateToDifferentTable() { // Create the and insert the person var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 }; var id = DB.Insert("SpecificPeople", "Id", person); // Update a few properties of the person person.Age = 70; person.Name = "The PetaPoco"; // Tell PetaPoco to update the DB table SpecificPeople // The update statement produced is `UPDATE [SpecificPeople] SET [FullName] = @0, [Age] = @1, [Height] = @2, [Dob] = @3 WHERE [Id] = @4` DB.Update("SpecificPeople", "Id", person); // We need to get the clone/copy from the correct table // Note: we can't use auto select builder here because PetaPoco would create columns such as People.Id var clone = DB.Query<Person>("SELECT * FROM [SpecificPeople] sp WHERE sp.[Id] = @0", id).Single(); // See, the person has been updated clone.Id.ShouldBe(person.Id); clone.Dob.ShouldBe(person.Dob); clone.Height.ShouldBe(person.Height); clone.Age.ShouldBe(person.Age); clone.Name.ShouldBe(person.Name); }
public void UpdatePartial() { // Create and insert the person var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 }; var id = DB.Insert(person); // Update a few properties of the person person.Age = 70; person.Name = "The PetaPoco"; // Get the poco data var pocoData = PocoData.ForType(person.GetType(), DB.DefaultMapper); // Tell PetaPoco to update only ther person's name // The update statement produced is `UPDATE [People] SET [FullName] = @0 WHERE [Id] = @1` DB.Update(person, new [] { pocoData.GetColumnName(nameof(Person.Name)) }); // Get a clone/copy from the DB var clone = DB.Single<Person>(id); // See, the person has been updated, but only the name clone.Id.ShouldBe(person.Id); clone.Dob.ShouldBe(person.Dob); clone.Height.ShouldBe(person.Height); clone.Age.ShouldNotBe(70); clone.Name.ShouldBe("The PetaPoco"); }
public void Insert() { // Create the person var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 }; // Tell PetaPoco to insert it var id = DB.Insert(person); // Obviously the ID returned will be the same at the one we set id.ShouldBe(person.Id); // Get a clone/copy from the DB var clone = DB.Single<Person>(id); // See, they're are the same clone.ShouldBe(person); // But, they're not not reference equals as PetaPoco doesn't cache because it's a MircoORM. person.Equals(clone).ShouldBeFalse(); }
public void InsertAutoIncrement() { var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 }; DB.Insert(person); // Create the order var order = new Order { PersonId = person.Id, PoNumber = "PETAPOCO", Status = OrderStatus.Pending, CreatedBy = "Office PetaPoco", CreatedOn = new DateTime(1948, 1, 11, 4, 2, 4, DateTimeKind.Utc) }; // Tell PetaPoco to insert it var id = DB.Insert(order); // PetaPoco updates the POCO's ID for us, see id.ShouldBe(order.Id); // Get a clone/copy from the DB var clone = DB.Single<Order>(id); // See, they're are the same clone.ShouldBe(order); // But, they're not not reference equals as PetaPoco doesn't cache because it's a MircoORM. order.Equals(clone).ShouldBeFalse(); }
public void Update() { // Create and insert the person var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 }; var id = DB.Insert(person); // Update a few properties of the person person.Age = 70; person.Name = "The PetaPoco"; // Tell PetaPoco to update the DB DB.Update(person); // Get a clone/copy from the DB var clone = DB.Single<Person>(id); // See, the person has been updated clone.Id.ShouldBe(person.Id); clone.Dob.ShouldBe(person.Dob); clone.Height.ShouldBe(person.Height); clone.Age.ShouldBe(person.Age); clone.Name.ShouldBe(person.Name); }
public void ShouldBe(Person other) { Id.ShouldBe(other.Id); Name.ShouldBe(other.Name); Age.ShouldBe(other.Age); Height.ShouldBe(other.Height); Dob.ShouldBe(other.Dob); }