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

Commit 5afc202

Browse files
committed
Select best matching reference field for foreign key annotated fields
1 parent bbbb7ff commit 5afc202

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

src/ServiceStack.OrmLite/OrmLiteReadCommandExtensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,12 @@ public static FieldDefinition GetRefFieldDef(this ModelDefinition modelDef, Mode
863863

864864
public static FieldDefinition GetRefFieldDefIfExists(this ModelDefinition modelDef, ModelDefinition refModelDef)
865865
{
866-
var refField = refModelDef.FieldDefinitions.FirstOrDefault(x => x.ForeignKey != null && x.ForeignKey.ReferenceType == modelDef.ModelType)
867-
?? refModelDef.FieldDefinitions.FirstOrDefault(x => x.FieldName == modelDef.ModelName + "Id")
868-
?? refModelDef.FieldDefinitions.FirstOrDefault(x => x.Name == modelDef.Name + "Id");
866+
var refField =
867+
refModelDef.FieldDefinitions.FirstOrDefault(x => x.ForeignKey != null && x.ForeignKey.ReferenceType == modelDef.ModelType
868+
&& (x.FieldName == modelDef.ModelName + "Id" || x.Name == modelDef.Name + "Id"))
869+
?? refModelDef.FieldDefinitions.FirstOrDefault(x => x.ForeignKey != null && x.ForeignKey.ReferenceType == modelDef.ModelType)
870+
?? refModelDef.FieldDefinitions.FirstOrDefault(x => x.FieldName == modelDef.ModelName + "Id")
871+
?? refModelDef.FieldDefinitions.FirstOrDefault(x => x.Name == modelDef.Name + "Id");
869872
return refField;
870873
}
871874

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using NUnit.Framework;
4+
using ServiceStack.DataAnnotations;
5+
using ServiceStack.Text;
6+
7+
namespace ServiceStack.OrmLite.Tests.Issues
8+
{
9+
public class Goal
10+
{
11+
[PrimaryKey]
12+
public long Id { get; set; }
13+
14+
[Reference]
15+
public List<Item> Items { get; set; }
16+
}
17+
18+
public class Item
19+
{
20+
[PrimaryKey]
21+
public long Id { get; set; }
22+
23+
[ForeignKey(typeof(Goal))]
24+
public long AnyGoalId { get; set; }
25+
26+
[Alias("CorrectGoalId")]
27+
[ForeignKey(typeof(Goal))]
28+
public long GoalId { get; set; }
29+
}
30+
31+
[TestFixture]
32+
public class MultiFieldReferenceTests
33+
: OrmLiteTestBase
34+
{
35+
[Test]
36+
public void Does_select_correct_reference_field()
37+
{
38+
using (var db = OpenDbConnection())
39+
{
40+
db.DropTable<Item>();
41+
db.DropTable<Goal>();
42+
db.CreateTable<Goal>();
43+
db.CreateTable<Item>();
44+
45+
var goal = new Goal { Id = 1 };
46+
db.Save(goal, references: true);
47+
48+
var goalWithItems = new Goal
49+
{
50+
Id = 2,
51+
Items = new List<Item>
52+
{
53+
new Item { Id = 10, AnyGoalId = 1 },
54+
new Item { Id = 11, AnyGoalId = 1 },
55+
}
56+
};
57+
58+
db.Save(goalWithItems, references: true);
59+
60+
Assert.That(goalWithItems.Items[0].GoalId, Is.EqualTo(goalWithItems.Id));
61+
Assert.That(goalWithItems.Items[1].GoalId, Is.EqualTo(goalWithItems.Id));
62+
63+
var dbGoals = db.LoadSelect<Goal>(x => x.Id == goalWithItems.Id).First();
64+
db.GetLastSql().Print();
65+
66+
Assert.That(dbGoals.Items[0].GoalId, Is.EqualTo(goalWithItems.Id));
67+
Assert.That(dbGoals.Items[1].GoalId, Is.EqualTo(goalWithItems.Id));
68+
}
69+
}
70+
}
71+
}

tests/ServiceStack.OrmLite.Tests/ServiceStack.OrmLite.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
<Compile Include="Expression\SelectExpressionTests.cs" />
131131
<Compile Include="Issues\ComplexJoinWithAlias.cs" />
132132
<Compile Include="Issues\MismatchSchemaTests.cs" />
133+
<Compile Include="Issues\MultiFieldReferenceTests.cs" />
133134
<Compile Include="Issues\MultithreadingIssueTests.cs" />
134135
<Compile Include="Issues\SchemaTests.cs" />
135136
<Compile Include="Issues\SelectDistinctTests.cs" />

0 commit comments

Comments
 (0)