public void UpdateConventionalUnconventionalPoco() { // Create the UnconventionalPocos table DB.Execute(@"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES t WHERE t.TABLE_SCHEMA = 'dbo' AND t.TABLE_NAME = 'TBL_UnconventionalPocos') DROP TABLE dbo.[TBL_UnconventionalPocos] CREATE TABLE dbo.[TBL_UnconventionalPocos] ( [PrimaryKey] INT IDENTITY(1,1) PRIMARY KEY, [Text] NTEXT NOT NULL )"); // Reconfigure the convention mapper // Note: I can't think of a valid reason, other than for a purpose such as this, where you would configure the convention mapper in this way. ((ConventionMapper)DB.DefaultMapper).MapPrimaryKey = (ti, t) => { var prop = t.GetProperties().FirstOrDefault(p => p.Name == "PrimaryKey"); if (prop == null) { return(false); } ti.PrimaryKey = prop.Name; ti.AutoIncrement = ((ConventionMapper)DB.DefaultMapper).IsPrimaryKeyAutoIncrement(prop.PropertyType); return(true); }; ((ConventionMapper)DB.DefaultMapper).InflectTableName = (i, tn) => "TBL_" + tn + "s"; // Create the POCO var poco = new UnconventionalPoco { Text = "PetaPoco" }; // Tell PetaPoco to insert it var id = DB.Insert(poco); // Get a clone/copy from the DB var clone = DB.SingleOrDefault <UnconventionalPoco>(id); // See, they're are the same clone.ShouldBe(poco); // Update the original poco poco.Text += " some more text"; // Update the poco DB.Update(poco); // Get the clone from teh database again clone = DB.SingleOrDefault <UnconventionalPoco>(id); // Confirm the text was updated clone.Text.ShouldBe("PetaPoco some more text"); }
public void ShouldBe(UnconventionalPoco other) { PrimaryKey.ShouldBe(other.PrimaryKey); Text.ShouldBe(other.Text); }
public void InsertUnconventionalPoco() { // Create the UnconventionalPocos table DB.Execute(@"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES t WHERE t.TABLE_SCHEMA = 'dbo' AND t.TABLE_NAME = 'TBL_UnconventionalPocos') DROP TABLE dbo.[TBL_UnconventionalPocos] CREATE TABLE dbo.[TBL_UnconventionalPocos] ( [PrimaryKey] INT IDENTITY(1,1) PRIMARY KEY, [Text] NTEXT NOT NULL )"); // This POCO is unconventional because, when using the default conventional mapper, PetaPoco won't understand how this poco maps to the database. // To understand the power how the conventional mapper, one could configure to work in this situation. var poco = new UnconventionalPoco { Text = "PetaPoco" }; // Insert the poco var id = DB.Insert("TBL_UnconventionalPocos", "PrimaryKey", true, poco); // Get a clone/copy from the DB var clone = DB.Query<UnconventionalPoco>("SELECT * FROM [TBL_UnconventionalPocos] WHERE [PrimaryKey] = @0", id).Single(); // See, they're are the same clone.ShouldBe(poco); // But, they're not not reference equals as PetaPoco doesn't cache because it's a MircoORM. poco.Equals(clone).ShouldBeFalse(); }
public void InsertConventionalUnconventionalPoco() { // Create the UnconventionalPocos table DB.Execute(@"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES t WHERE t.TABLE_SCHEMA = 'dbo' AND t.TABLE_NAME = 'TBL_UnconventionalPocos') DROP TABLE dbo.[TBL_UnconventionalPocos] CREATE TABLE dbo.[TBL_UnconventionalPocos] ( [PrimaryKey] INT IDENTITY(1,1) PRIMARY KEY, [Text] NTEXT NOT NULL )"); // Reconfigure the convention mapper // Note: I can't think of a valid reason, other than for a purpose such as this, where you would configure the convention mapper in this way. ((ConventionMapper) DB.DefaultMapper).MapPrimaryKey = (ti, t) => { var prop = t.GetProperties().FirstOrDefault(p => p.Name == "PrimaryKey"); if (prop == null) return false; ti.PrimaryKey = prop.Name; ti.AutoIncrement = ((ConventionMapper)DB.DefaultMapper).IsPrimaryKeyAutoIncrement(prop.PropertyType); return true; }; ((ConventionMapper) DB.DefaultMapper).InflectTableName = (i, tn) => "TBL_" + tn + "s"; // Create the POCO var poco = new UnconventionalPoco { Text = "PetaPoco" }; // Tell PetaPoco to insert it var id = DB.Insert(poco); // Get a clone/copy from the DB var clone = DB.SingleOrDefault<UnconventionalPoco>(id); // See, they're are the same clone.ShouldBe(poco); // But, they're not not reference equals as PetaPoco doesn't cache because it's a MircoORM. poco.Equals(clone).ShouldBeFalse(); }
public void UpdateUnconventionalPoco() { // Create the UnconventionalPocos table DB.Execute(@"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES t WHERE t.TABLE_SCHEMA = 'dbo' AND t.TABLE_NAME = 'TBL_UnconventionalPocos') DROP TABLE dbo.[TBL_UnconventionalPocos] CREATE TABLE dbo.[TBL_UnconventionalPocos] ( [PrimaryKey] INT IDENTITY(1,1) PRIMARY KEY, [Text] NTEXT NOT NULL )"); // This POCO is unconventional because, when using the default conventional mapper, PetaPoco won't understand how this poco maps to the database. // To understand the power of unconventional mapping, a developer could configure it to work in this situation. var poco = new UnconventionalPoco { Text = "PetaPoco" }; // Insert the poco var id = DB.Insert("TBL_UnconventionalPocos", "PrimaryKey", true, poco); // Update the poco poco.Text += " some more text"; DB.Update("TBL_UnconventionalPocos", "PrimaryKey", poco); // Get a clone/copy from the DB var clone = DB.Query<UnconventionalPoco>("SELECT * FROM [TBL_UnconventionalPocos] WHERE [PrimaryKey] = @0", id).Single(); // Just to be sure poco.PrimaryKey.ShouldBe(clone.PrimaryKey); poco.Text.ShouldBe(clone.Text); }