diff --git a/Dapper.Contrib.Tests/Assert.cs b/Dapper.Contrib.Tests/Assert.cs index 028b5e42..3b6b75d1 100644 --- a/Dapper.Contrib.Tests/Assert.cs +++ b/Dapper.Contrib.Tests/Assert.cs @@ -64,6 +64,13 @@ namespace Dapper.Contrib.Tests throw new ApplicationException("Expected null"); } } + public static void IsNotNull(this object obj) + { + if (obj == null) + { + throw new ApplicationException("Expected not null"); + } + } } } \ No newline at end of file diff --git a/Dapper.Contrib.Tests/Program.cs b/Dapper.Contrib.Tests/Program.cs index f12d5d47..a910ec5e 100644 --- a/Dapper.Contrib.Tests/Program.cs +++ b/Dapper.Contrib.Tests/Program.cs @@ -28,6 +28,7 @@ namespace Dapper.Contrib.Tests using (var connection = new SqlCeConnection(connectionString)) { connection.Open(); + connection.Execute(@" create table Stuff (TheId int IDENTITY(1,1) not null, Name nvarchar(100) not null, Created DateTime null) "); connection.Execute(@" create table People (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) "); connection.Execute(@" create table Users (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, Age int not null) "); connection.Execute(@" create table Automobiles (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) "); diff --git a/Dapper.Contrib.Tests/Tests.cs b/Dapper.Contrib.Tests/Tests.cs index f8510ec4..3ca49c87 100644 --- a/Dapper.Contrib.Tests/Tests.cs +++ b/Dapper.Contrib.Tests/Tests.cs @@ -33,6 +33,15 @@ namespace Dapper.Contrib.Tests public string Name { get; set; } } + [Table("Stuff")] + public class Stuff + { + [Key] + public short TheId { get; set; } + public string Name { get; set; } + public DateTime? Created { get; set; } + } + [Table("Automobiles")] public class Car { @@ -71,6 +80,30 @@ namespace Dapper.Contrib.Tests return connection; } + public void ShortIdentity() + { + using (var connection = GetOpenConnection()) + { + var id = connection.Insert(new Stuff() { Name = "First item" }); + id.IsEqualTo(1); + var item = connection.Get(1); + item.TheId.IsEqualTo((short)1); + } + } + + public void NullDateTime() + { + using (var connection = GetOpenConnection()) + { + connection.Insert(new Stuff() { Name = "First item" }); + connection.Insert(new Stuff() { Name = "Second item", Created = DateTime.Now }); + var stuff = connection.Query("select * from stuff").ToList(); + stuff.First().Created.IsNull(); + stuff.Last().Created.IsNotNull(); + + } + } + public void TableName() { using (var connection = GetOpenConnection()) @@ -96,14 +129,9 @@ namespace Dapper.Contrib.Tests connection.Delete(user); } } - + public void TestClosedConnection() { - //using (var connection = GetOpenConnection()) - //{ - // connection.Insert(new User { Name = "Adama", Age = 10 }).IsMoreThan(0); - //} - using (var connection = GetConnection()) { connection.Insert(new User { Name = "Adama", Age = 10 }).IsMoreThan(0); diff --git a/Dapper.Contrib/SqlMapperExtensions.cs b/Dapper.Contrib/SqlMapperExtensions.cs index 00436334..1b2f222c 100644 --- a/Dapper.Contrib/SqlMapperExtensions.cs +++ b/Dapper.Contrib/SqlMapperExtensions.cs @@ -223,7 +223,7 @@ namespace Dapper.Contrib.Extensions { string name; if (TypeTableName.TryGetValue(type.TypeHandle, out name)) return name; - + if (TableNameMapper != null) { name = TableNameMapper(type); @@ -246,7 +246,7 @@ namespace Dapper.Contrib.Extensions return name; } - + /// /// Inserts an entity into table "Ts" and returns identity id or number if inserted rows if inserting a list. /// @@ -418,7 +418,7 @@ namespace Dapper.Contrib.Extensions /// Please note that this callback is global and will be used by all the calls that require a database specific adapter. /// public static GetDatabaseTypeDelegate GetDatabaseType; - + private static ISqlAdapter GetFormatter(IDbConnection connection) { @@ -634,21 +634,29 @@ public partial class SqlServerAdapter : ISqlAdapter public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, String tableName, string columnList, string parameterList, IEnumerable keyProperties, object entityToInsert) { //TODO: Append a select identity at end if command? + //TODO: Create specific sqladapter for sqlce var cmd = String.Format("insert into {0} ({1}) values ({2})", tableName, columnList, parameterList); connection.Execute(cmd, entityToInsert, transaction, commandTimeout); - //NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE + //TODO: use IDENT_CURRENT('tablename') or IDENT_SCOPE or SCOPE_IDENTITY() - create specific sqlce adapter with @@IDENTITY var r = connection.Query("select @@IDENTITY id", transaction: transaction, commandTimeout: commandTimeout); - var id = (int)r.First().id; + var id = r.First().id; var propertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray(); if (propertyInfos.Any()) - propertyInfos.First().SetValue(entityToInsert, id, null); - return id; + { + var idProperty = propertyInfos.First(); + if (idProperty.PropertyType.Name == "Int16") //for short id/key types issue #196 + idProperty.SetValue(entityToInsert, (Int16)id, null); + else + idProperty.SetValue(entityToInsert, (int)id, null); + } + return (int)id; } } + public partial class PostgresAdapter : ISqlAdapter { public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, String tableName, string columnList, string parameterList, IEnumerable keyProperties, object entityToInsert)