Skip to content

Commit e24c472

Browse files
committed
Merge branch 'johandanforth-bug-in-keypropertiescache'
2 parents 07572a2 + eb51910 commit e24c472

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

Dapper.Contrib/SqlMapperExtensions.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,15 @@ private static List<PropertyInfo> KeyPropertiesCache(Type type)
9191
}
9292

9393
var allProperties = TypePropertiesCache(type);
94-
var keyProperties = allProperties.Where(p => p.GetCustomAttributes(true).Any(a => a is KeyAttribute)).ToList();
94+
var keyProperties = allProperties.Where(p =>
95+
{
96+
return p.GetCustomAttributes(true).Any(a => a is KeyAttribute);
97+
}).ToList();
9598

9699
if (keyProperties.Count == 0)
97100
{
98101
var idProp = allProperties.FirstOrDefault(p => p.Name.ToLower() == "id");
99-
if (idProp != null)
102+
if (idProp != null && !idProp.GetCustomAttributes(true).Any(a => a is ExplicitKeyAttribute))
100103
{
101104
keyProperties.Add(idProp);
102105
}

Dapper.Tests.Contrib/TestSuite.cs

+32-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ public class ObjectY
3434
public int ObjectYId { get; set; }
3535
public string Name { get; set; }
3636
}
37+
38+
[Table("ObjectZ")]
39+
public class ObjectZ
40+
{
41+
[ExplicitKey]
42+
public int Id { get; set; }
43+
public string Name { get; set; }
44+
}
3745

3846
public interface IUser
3947
{
@@ -134,7 +142,7 @@ public void InsertGetUpdateDeleteWithExplicitKey()
134142
o2.IsNull();
135143
}
136144
}
137-
145+
138146
[Fact]
139147
public void GetAllWithExplicitKey()
140148
{
@@ -149,7 +157,29 @@ public void GetAllWithExplicitKey()
149157
}
150158
}
151159

152-
[Fact]
160+
[Fact]
161+
public void InsertGetUpdateDeleteWithExplicitKeyNamedId()
162+
{
163+
using (var connection = GetOpenConnection())
164+
{
165+
const int id = 42;
166+
var o2 = new ObjectZ { Id = id, Name = "Foo" };
167+
connection.Insert(o2);
168+
var list2 = connection.Query<ObjectZ>("select * from ObjectZ").ToList();
169+
list2.Count.IsEqualTo(1);
170+
o2 = connection.Get<ObjectZ>(id);
171+
o2.Id.IsEqualTo(id);
172+
//o2.Name = "Bar";
173+
//connection.Update(o2);
174+
//o2 = connection.Get<ObjectY>(id);
175+
//o2.Name.IsEqualTo("Bar");
176+
//connection.Delete(o2);
177+
//o2 = connection.Get<ObjectY>(id);
178+
//o2.IsNull();
179+
}
180+
}
181+
182+
[Fact]
153183
public void ShortIdentity()
154184
{
155185
using (var connection = GetOpenConnection())

Dapper.Tests.Contrib/TestSuites.cs

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ static SqlServerTestSuite()
5555
connection.Execute(@"CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null);");
5656
dropTable("ObjectY");
5757
connection.Execute(@"CREATE TABLE ObjectY (ObjectYId int not null, Name nvarchar(100) not null);");
58+
dropTable("ObjectZ");
59+
connection.Execute(@"CREATE TABLE ObjectZ (Id int not null, Name nvarchar(100) not null);");
5860
}
5961
}
6062
}
@@ -101,6 +103,8 @@ static MySqlServerTestSuite()
101103
connection.Execute(@"CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null);");
102104
dropTable("ObjectY");
103105
connection.Execute(@"CREATE TABLE ObjectY (ObjectYId int not null, Name nvarchar(100) not null);");
106+
dropTable("ObjectZ");
107+
connection.Execute(@"CREATE TABLE ObjectZ (Id int not null, Name nvarchar(100) not null);");
104108
}
105109
}
106110
catch (MySqlException e)
@@ -141,6 +145,7 @@ static SQLiteTestSuite()
141145
connection.Execute(@"CREATE TABLE Results (Id integer primary key autoincrement not null, Name nvarchar(100) not null, [Order] int not null) ");
142146
connection.Execute(@"CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null) ");
143147
connection.Execute(@"CREATE TABLE ObjectY (ObjectYId integer not null, Name nvarchar(100) not null) ");
148+
connection.Execute(@"CREATE TABLE ObjectZ (Id integer not null, Name nvarchar(100) not null) ");
144149
}
145150
}
146151
}
@@ -171,6 +176,7 @@ static SqlCETestSuite()
171176
connection.Execute(@"CREATE TABLE Results (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, [Order] int not null) ");
172177
connection.Execute(@"CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null) ");
173178
connection.Execute(@"CREATE TABLE ObjectY (ObjectYId int not null, Name nvarchar(100) not null) ");
179+
connection.Execute(@"CREATE TABLE ObjectZ (Id int not null, Name nvarchar(100) not null) ");
174180
}
175181
Console.WriteLine("Created database");
176182
}

0 commit comments

Comments
 (0)