Skip to content

Commit

Permalink
Merge branch 'johandanforth-bug-in-keypropertiescache'
Browse files Browse the repository at this point in the history
  • Loading branch information
NickCraver committed Nov 26, 2015
2 parents 07572a2 + eb51910 commit e24c472
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Dapper.Contrib/SqlMapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ private static List<PropertyInfo> KeyPropertiesCache(Type type)
}

var allProperties = TypePropertiesCache(type);
var keyProperties = allProperties.Where(p => p.GetCustomAttributes(true).Any(a => a is KeyAttribute)).ToList();
var keyProperties = allProperties.Where(p =>
{
return p.GetCustomAttributes(true).Any(a => a is KeyAttribute);
}).ToList();

if (keyProperties.Count == 0)
{
var idProp = allProperties.FirstOrDefault(p => p.Name.ToLower() == "id");
if (idProp != null)
if (idProp != null && !idProp.GetCustomAttributes(true).Any(a => a is ExplicitKeyAttribute))
{
keyProperties.Add(idProp);
}
Expand Down
34 changes: 32 additions & 2 deletions Dapper.Tests.Contrib/TestSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public class ObjectY
public int ObjectYId { get; set; }
public string Name { get; set; }
}

[Table("ObjectZ")]
public class ObjectZ
{
[ExplicitKey]
public int Id { get; set; }
public string Name { get; set; }
}

public interface IUser
{
Expand Down Expand Up @@ -134,7 +142,7 @@ public void InsertGetUpdateDeleteWithExplicitKey()
o2.IsNull();
}
}

[Fact]
public void GetAllWithExplicitKey()
{
Expand All @@ -149,7 +157,29 @@ public void GetAllWithExplicitKey()
}
}

[Fact]
[Fact]
public void InsertGetUpdateDeleteWithExplicitKeyNamedId()
{
using (var connection = GetOpenConnection())
{
const int id = 42;
var o2 = new ObjectZ { Id = id, Name = "Foo" };
connection.Insert(o2);
var list2 = connection.Query<ObjectZ>("select * from ObjectZ").ToList();
list2.Count.IsEqualTo(1);
o2 = connection.Get<ObjectZ>(id);
o2.Id.IsEqualTo(id);
//o2.Name = "Bar";
//connection.Update(o2);
//o2 = connection.Get<ObjectY>(id);
//o2.Name.IsEqualTo("Bar");
//connection.Delete(o2);
//o2 = connection.Get<ObjectY>(id);
//o2.IsNull();
}
}

[Fact]
public void ShortIdentity()
{
using (var connection = GetOpenConnection())
Expand Down
6 changes: 6 additions & 0 deletions Dapper.Tests.Contrib/TestSuites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ static SqlServerTestSuite()
connection.Execute(@"CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null);");
dropTable("ObjectY");
connection.Execute(@"CREATE TABLE ObjectY (ObjectYId int not null, Name nvarchar(100) not null);");
dropTable("ObjectZ");
connection.Execute(@"CREATE TABLE ObjectZ (Id int not null, Name nvarchar(100) not null);");
}
}
}
Expand Down Expand Up @@ -101,6 +103,8 @@ static MySqlServerTestSuite()
connection.Execute(@"CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null);");
dropTable("ObjectY");
connection.Execute(@"CREATE TABLE ObjectY (ObjectYId int not null, Name nvarchar(100) not null);");
dropTable("ObjectZ");
connection.Execute(@"CREATE TABLE ObjectZ (Id int not null, Name nvarchar(100) not null);");
}
}
catch (MySqlException e)
Expand Down Expand Up @@ -141,6 +145,7 @@ static SQLiteTestSuite()
connection.Execute(@"CREATE TABLE Results (Id integer primary key autoincrement not null, Name nvarchar(100) not null, [Order] int not null) ");
connection.Execute(@"CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null) ");
connection.Execute(@"CREATE TABLE ObjectY (ObjectYId integer not null, Name nvarchar(100) not null) ");
connection.Execute(@"CREATE TABLE ObjectZ (Id integer not null, Name nvarchar(100) not null) ");
}
}
}
Expand Down Expand Up @@ -171,6 +176,7 @@ static SqlCETestSuite()
connection.Execute(@"CREATE TABLE Results (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, [Order] int not null) ");
connection.Execute(@"CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null) ");
connection.Execute(@"CREATE TABLE ObjectY (ObjectYId int not null, Name nvarchar(100) not null) ");
connection.Execute(@"CREATE TABLE ObjectZ (Id int not null, Name nvarchar(100) not null) ");
}
Console.WriteLine("Created database");
}
Expand Down

0 comments on commit e24c472

Please sign in to comment.