Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit c575202

Browse files
committed
Add SQL Server test to retrieve generated Server GUID
1 parent 4d6905f commit c575202

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/ServiceStack.OrmLite.SqlServerTests/InsertParam_GetLastInsertId.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Linq;
44
using System.Text;
55
using NUnit.Framework;
6+
using ServiceStack.DataAnnotations;
7+
using ServiceStack.OrmLite.Dapper;
68

79
namespace ServiceStack.OrmLite.SqlServerTests
810
{
@@ -27,9 +29,44 @@ public void Can_GetLastInsertedId_using_InsertParam()
2729
{
2830
con.CreateTable<SimpleType>(true);
2931

30-
var lastInsertId = con.Insert(testObject, selectIdentity:true);
32+
var lastInsertId = con.Insert(testObject, selectIdentity: true);
3133
Assert.That(lastInsertId, Is.GreaterThan(0), "with InsertParam");
3234
}
3335
}
36+
37+
public class ServerGuid
38+
{
39+
[Default(typeof(Guid), "newid()")]
40+
public Guid Id { get; set; }
41+
42+
public string Name { get; set; }
43+
}
44+
45+
[Test]
46+
public void Can_retrieve_ServerGuid()
47+
{
48+
using (var db = OpenDbConnection())
49+
using (var cmd = db.CreateCommand())
50+
{
51+
db.DropAndCreateTable<ServerGuid>();
52+
53+
var obj = new ServerGuid { Name = "foo" };
54+
55+
cmd.GetDialectProvider().PrepareParameterizedInsertStatement<ServerGuid>(cmd,
56+
insertFields: OrmLiteUtils.GetNonDefaultValueInsertFields(obj));
57+
58+
cmd.CommandText = cmd.CommandText.Replace("VALUES", "OUTPUT inserted.Id VALUES");
59+
60+
cmd.GetDialectProvider().SetParameterValues<ServerGuid>(cmd, obj);
61+
62+
var id = (Guid)cmd.ExecuteScalar();
63+
64+
Assert.That(id, Is.Not.EqualTo(default(Guid)));
65+
66+
var insertedRow = db.SingleById<ServerGuid>(id);
67+
68+
Assert.That(insertedRow.Name, Is.EqualTo("foo"));
69+
}
70+
}
3471
}
3572
}

0 commit comments

Comments
 (0)