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

Commit e891ef3

Browse files
committed
Expand fallback for JOIN selects to be able to reference any {Table}{Field} property convention
1 parent 055d3c6 commit e891ef3

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.Join.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Linq.Expressions;
45
using System.Text;
56

@@ -154,22 +155,23 @@ public string SelectInto<TModel>()
154155

155156
if (!found)
156157
{
157-
// Add support for auto mapping `{Table}Id` convention
158+
// Add support for auto mapping `{Table}{Field}` convention
158159
foreach (var tableDef in tableDefs)
159160
{
160-
var primaryKey = tableDef.PrimaryKey;
161-
if (primaryKey == null) continue;
162-
163-
var tableId = tableDef.Name + primaryKey.Name;
164-
if (fieldDef.Name == tableId)
161+
var tableName = tableDef.Name;
162+
var matchingField = tableDef.FieldDefinitionsArray
163+
.FirstOrDefault(x => tableName + x.FieldName == fieldDef.Name);
164+
165+
if (matchingField != null)
165166
{
166167
if (sbSelect.Length > 0)
167168
sbSelect.Append(", ");
168169

169170
sbSelect.AppendFormat("{0}.{1} as {2}",
170171
SqlTable(tableDef.ModelName),
171-
primaryKey.GetQuotedName(DialectProvider),
172+
matchingField.GetQuotedName(DialectProvider),
172173
fieldDef.Name);
174+
173175
break;
174176
}
175177
}

tests/ServiceStack.OrmLite.Tests/LoadReferencesJoinTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ public class FullCustomerInfo
6464
{
6565
public int Id { get; set; }
6666
public string Name { get; set; }
67+
public string CustomerName { get; set; }
6768
public int CustomerAddressId { get; set; }
6869
public string AddressLine1 { get; set; }
6970
public string City { get; set; }
7071
public int OrderId { get; set; }
7172
public string LineItem { get; set; }
7273
public decimal Cost { get; set; }
74+
public decimal OrderCost { get; set; }
7375
public string CountryCode { get; set; }
7476
}
7577

@@ -366,7 +368,7 @@ public void Can_Join_on_matching_Alias_convention()
366368
}
367369

368370
[Test]
369-
public void Does_populate_PrimaryKey_ids_based_on_property_convention()
371+
public void Does_populate_custom_columns_based_on_property_convention()
370372
{
371373
// Reset auto ids
372374
db.DropAndCreateTable<Order>();
@@ -385,6 +387,12 @@ public void Does_populate_PrimaryKey_ids_based_on_property_convention()
385387
var orderIds = results.ConvertAll(x => x.OrderId);
386388
Assert.That(orderIds, Is.EquivalentTo(new[] { 1, 2 }));
387389

390+
var customerNames = results.ConvertAll(x => x.CustomerName);
391+
Assert.That(customerNames, Is.EquivalentTo(new[] { "Customer 1", "Customer 1" }));
392+
393+
var orderCosts = results.ConvertAll(x => x.OrderCost);
394+
Assert.That(orderCosts, Is.EquivalentTo(new[] { 1.99m, 2.99m }));
395+
388396
var expr = db.From<Customer>()
389397
.Join<Customer, CustomerAddress>()
390398
.Join<Customer, Order>()
@@ -397,6 +405,12 @@ public void Does_populate_PrimaryKey_ids_based_on_property_convention()
397405

398406
orderIds = results.ConvertAll(x => x.OrderId);
399407
Assert.That(orderIds, Is.EquivalentTo(new[] { 2 }));
408+
409+
customerNames = results.ConvertAll(x => x.CustomerName);
410+
Assert.That(customerNames, Is.EquivalentTo(new[] { "Customer 1" }));
411+
412+
orderCosts = results.ConvertAll(x => x.OrderCost);
413+
Assert.That(orderCosts, Is.EquivalentTo(new[] { 2.99m }));
400414
}
401415
}
402416
}

0 commit comments

Comments
 (0)