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

Commit 8ba9da5

Browse files
committed
Add fallback in Save/Load references to look at code conventions when matching aliases were not found
1 parent 3ec34c1 commit 8ba9da5

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/ServiceStack.OrmLite/OrmLiteReadExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,8 @@ public static void LoadReferences<T>(this IDbCommand dbCmd, T instance)
842842
private static FieldDefinition GetRefFieldDef(ModelDefinition modelDef, ModelDefinition refModelDef, Type refType)
843843
{
844844
var refNameConvention = modelDef.ModelName + "Id";
845-
var refField = refModelDef.FieldDefinitions.FirstOrDefault(x => x.FieldName == refNameConvention);
845+
var refField = refModelDef.FieldDefinitions.FirstOrDefault(x => x.FieldName == refNameConvention)
846+
?? refModelDef.FieldDefinitions.FirstOrDefault(x => x.Name == modelDef.Name + "Id");
846847
if (refField == null)
847848
throw new ArgumentException("Cant find '{0}' Property on Type '{1}'".Fmt(refNameConvention, refType.Name));
848849
return refField;

tests/ServiceStack.OrmLite.Tests/LoadReferencesTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,31 @@ public class OldAliasedCustomerAddress
105105
public string Country { get; set; }
106106
}
107107

108+
[Alias("FooCustomer")]
109+
public class MismatchAliasCustomer
110+
{
111+
[AutoIncrement]
112+
public int Id { get; set; }
113+
public string Name { get; set; }
114+
115+
[Reference]
116+
public MismatchAliasAddress PrimaryAddress { get; set; }
117+
}
118+
119+
[Alias("BarCustomerAddress")]
120+
public class MismatchAliasAddress
121+
{
122+
[AutoIncrement]
123+
public int Id { get; set; }
124+
[Alias("BarCustomerId")]
125+
public int MismatchAliasCustomerId { get; set; }
126+
public string AddressLine1 { get; set; }
127+
public string AddressLine2 { get; set; }
128+
public string City { get; set; }
129+
public string State { get; set; }
130+
public string Country { get; set; }
131+
}
132+
108133
public class LoadReferencesTests
109134
: OrmLiteTestBase
110135
{
@@ -122,6 +147,8 @@ public class LoadReferencesTests
122147
db.DropAndCreateTable<AliasedCustomerAddress>();
123148
db.DropAndCreateTable<OldAliasedCustomer>();
124149
db.DropAndCreateTable<OldAliasedCustomerAddress>();
150+
db.DropAndCreateTable<MismatchAliasCustomer>();
151+
db.DropAndCreateTable<MismatchAliasAddress>();
125152
}
126153

127154
[TestFixtureTearDown]
@@ -236,6 +263,36 @@ public void Can_Save_and_Load_Old_Aliased_References()
236263
Assert.That(dbCustomer.PrimaryAddress, Is.Not.Null);
237264
}
238265

266+
[Test]
267+
public void Can_Save_and_Load_MismatchedAlias_References_using_code_conventions()
268+
{
269+
var customer = new MismatchAliasCustomer
270+
{
271+
Name = "Customer 1",
272+
PrimaryAddress = new MismatchAliasAddress
273+
{
274+
AddressLine1 = "1 Humpty Street",
275+
City = "Humpty Doo",
276+
State = "Northern Territory",
277+
Country = "Australia"
278+
},
279+
};
280+
281+
db.Save(customer);
282+
283+
Assert.That(customer.Id, Is.GreaterThan(0));
284+
Assert.That(customer.PrimaryAddress.MismatchAliasCustomerId, Is.EqualTo(0));
285+
286+
db.SaveReferences(customer, customer.PrimaryAddress);
287+
Assert.That(customer.PrimaryAddress.MismatchAliasCustomerId, Is.EqualTo(customer.Id));
288+
289+
var dbCustomer = db.LoadSingleById<MismatchAliasCustomer>(customer.Id);
290+
291+
dbCustomer.PrintDump();
292+
293+
Assert.That(dbCustomer.PrimaryAddress, Is.Not.Null);
294+
}
295+
239296
[Test]
240297
public void Can_SaveAllReferences_then_Load_them()
241298
{

0 commit comments

Comments
 (0)