Last active
August 29, 2015 14:08
-
-
Save jklemmack/053c52ad4565478ce596 to your computer and use it in GitHub Desktop.
ServiceStack.OrmLite LoadSelect with OrderBy, and Skip == 0 doesn't always load correct child references
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT "Id", "Value" FROM "Child" WHERE "Id" IN (SELECT TOP 1 "ChildId" | |
FROM "Parent" ORDER BY "Id" DESC) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT "Id", "Value" FROM "Child" WHERE "Id" IN (SELECT TOP 1 "ChildId" | |
FROM "Parent") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using ServiceStack.DataAnnotations; | |
using ServiceStack.OrmLite; | |
namespace SSLoadSelectTest | |
{ | |
public class Parent | |
{ | |
[PrimaryKey] | |
public int Id { get; set; } | |
[References(typeof(Child))] | |
public int? ChildId { get; set; } | |
[Reference] | |
public Child Child { get; set; } | |
} | |
public class Child | |
{ | |
[PrimaryKey] | |
public int Id { get; set; } | |
public string Value { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
String strAppDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); | |
OrmLiteConfig.DialectProvider = SqlServerDialect.Provider; | |
string sqlConn = string.Format(@"Server=(localdb)\v11.0;Integrated Security=true;AttachDbFileName={0}\Database2.mdf;", strAppDir); | |
IDbConnection Db = SqlServerDialect.Provider.CreateConnection(sqlConn, new Dictionary<string, string>()); | |
Db.Open(); | |
Db.CreateTable<Child>(); | |
Db.CreateTable<Parent>(); | |
Db.Save<Child>(new Child() { Id = 1, Value = "Lolz" }); | |
Db.Insert<Parent>(new Parent() { Id = 1, ChildId = null }); | |
Db.Insert<Parent>(new Parent() { Id = 2, ChildId = 1 }); | |
// Select the Parent.Id == 2. LoadSelect should populate the child, but doesn't. | |
var exp = Db.From<Parent>() | |
.Limit(0, 1) | |
.OrderByDescending<Parent>(p=>p.Id) | |
; | |
try | |
{ | |
var results2 = Db.LoadSelect<Parent>(exp); | |
// Assert.IsNotNull(results2[0].Child) // <-- fails | |
} | |
catch (Exception ex) | |
{ | |
throw; | |
} | |
finally | |
{ | |
Db.DropTable<Parent>(); | |
Db.DropTable<Child>(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment