提交 ba752fb4 编写于 作者: W Wade Baglin

Add save documentation

上级 0aaa8855
using System;
using System.Linq;
using PetaPoco.Core;
using PetaPoco.Tests.Integration.Databases;
using PetaPoco.Tests.Integration.Databases.MSSQL;
......@@ -26,17 +25,17 @@ namespace PetaPoco.Tests.Integration.Documentation
// Tell PetaPoco to insert it
DB.Insert(person);
// Obviously, we find only 1 matching person in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @Id", new { person.Id });
count.ShouldBe(1);
// Tell PetaPoco to delete it
DB.Delete(person);
// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @0", person.Id);
count.ShouldBe(0);
count.ShouldBe(0);
}
[Fact]
......@@ -44,78 +43,78 @@ namespace PetaPoco.Tests.Integration.Documentation
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");
// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Insert(note);
// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);
// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);
// Now, tell PetaPoco to delete a note with the id of 1
DB.Delete<Note>(note.Id);
// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
count.ShouldBe(0);
}
[Fact]
public void DeleteCustomWhere()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");
// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Insert(note);
// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);
// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);
// Now, we'll tell PetaPoco how to delete the note
DB.Delete<Note>("WHERE [Id] = @Id", new { note.Id });
// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
count.ShouldBe(0);
}
[Fact]
public void DeleteCustomSqlWhere()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");
// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Insert(note);
// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);
// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);
// Now, we'll tell PetaPoco how to delete the note
var sql = new Sql();
sql.Where("[Id] = @Id", new { note.Id });
DB.Delete<Note>(sql);
// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
count.ShouldBe(0);
}
[Fact]
public void DeleteAdvanced()
{
......@@ -124,17 +123,17 @@ namespace PetaPoco.Tests.Integration.Documentation
// Tell PetaPoco to insert it, but to the table SpecificPeople and not People
DB.Insert("SpecificPeople", person);
// Obviously, we find only 1 matching person in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [SpecificPeople] WHERE [Id] = @Id", new { person.Id });
count.ShouldBe(1);
// Tell PetaPoco to delete it, but in the table SpecificPeople and not People
DB.Delete("SpecificPeople", "Id", person);
// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [SpecificPeople] WHERE [Id] = @0", person.Id);
count.ShouldBe(0);
count.ShouldBe(0);
}
}
}
\ No newline at end of file
using System;
using System.Data;
using System.Data.SqlClient;
namespace PetaPoco.Tests.Integration.Documentation.Pocos
{
......@@ -6,8 +8,21 @@ namespace PetaPoco.Tests.Integration.Documentation.Pocos
{
public int Id { get; set; }
[DateTime2ConverterAttribute]
public DateTime CreatedOn { get; set; }
public string Text { get; set; }
}
public class DateTime2ConverterAttribute : ValueConverterAttribute
{
public override object ConvertToDb(object value) =>
new SqlParameter
{
DbType = DbType.DateTime2,
Value = value
};
public override object ConvertFromDb(object value) => value;
}
}
\ No newline at end of file
using System;
using PetaPoco.Core;
using PetaPoco.Tests.Integration.Databases;
using PetaPoco.Tests.Integration.Databases.MSSQL;
using PetaPoco.Tests.Integration.Documentation.Pocos;
using Shouldly;
using Xunit;
namespace PetaPoco.Tests.Integration.Documentation
{
[Collection("MssqlTests")]
public class Save : BaseDatabase
{
public Save()
: base(new MssqlDBTestProvider())
{
PocoData.FlushCaches();
}
[Fact]
public void Save_Insert()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");
// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Save(note);
// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);
// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);
// Fetch a new copy of note
var noteFromDb = DB.Single<Note>(note.Id);
// They are the same
note.Id.ShouldBe(noteFromDb.Id);
note.Text.ShouldBe(noteFromDb.Text);
note.CreatedOn.Ticks.ShouldBe(noteFromDb.CreatedOn.Ticks);
}
[Fact]
public void Save_Update()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");
// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Save(note);
// Update the note
note.Text += " and this is my update";
DB.Save(note);
// Fetch a new copy of note
var noteFromDb = DB.Single<Note>(note.Id);
// The note text is the same
note.Text.ShouldBe(noteFromDb.Text);
note.Text.ShouldContain(" and this is my update");
}
}
}
\ No newline at end of file
......@@ -9,6 +9,10 @@
<Copyright>Collaborating Platypus</Copyright>
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugType>full</DebugType>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
<Compile Remove="Databases\MSAccess\**" />
......
......@@ -20,7 +20,7 @@ CREATE TABLE dbo.[People] (
[FullName] NVARCHAR(255),
[Age] BIGINT NOT NULL,
[Height] INT NOT NULL,
[Dob] DATETIME NULL
[Dob] DATETIME2 NULL
)
CREATE TABLE dbo.[Orders] (
......@@ -28,7 +28,7 @@ CREATE TABLE dbo.[Orders] (
[PersonId] UNIQUEIDENTIFIER FOREIGN KEY REFERENCES dbo.[People](Id),
[PoNumber] NVARCHAR(15) NOT NULL,
[OrderStatus] INT NOT NULL,
[CreatedOn] DATETIME NOT NULL,
[CreatedOn] DATETIME2 NOT NULL,
[CreatedBy] NVARCHAR(255) NOT NULL
)
......@@ -45,7 +45,7 @@ CREATE TABLE dbo.[SpecificPeople] (
[FullName] NVARCHAR(255),
[Age] BIGINT NOT NULL,
[Height] INT NOT NULL,
[Dob] DATETIME NULL
[Dob] DATETIME2 NULL
)
CREATE TABLE dbo.[SpecificOrders] (
......@@ -53,7 +53,7 @@ CREATE TABLE dbo.[SpecificOrders] (
[PersonId] UNIQUEIDENTIFIER FOREIGN KEY REFERENCES dbo.[SpecificPeople](Id),
[PoNumber] NVARCHAR(15) NOT NULL,
[OrderStatus] INT NOT NULL,
[CreatedOn] DATETIME NOT NULL,
[CreatedOn] DATETIME2 NOT NULL,
[CreatedBy] NVARCHAR(255) NOT NULL
)
......@@ -67,13 +67,13 @@ CREATE TABLE dbo.[SpecificOrderLines] (
CREATE TABLE dbo.[TransactionLogs] (
[Description] NTEXT,
[CreatedOn] DATETIME NOT NULL
[CreatedOn] DATETIME2 NOT NULL
)
CREATE TABLE dbo.[Note] (
[Id] INT IDENTITY(1,1) PRIMARY KEY,
[Text] NTEXT NOT NULL,
[CreatedOn] DATETIME NOT NULL
[CreatedOn] DATETIME2 NOT NULL
)
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'store')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册