Skip to content

Commit c1f9ad3

Browse files
committed
#56 enhanced to provide error feedback
1 parent c74b40f commit c1f9ad3

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

SubSonic.Core.DataAccessLayer/src/Builders/DbSqlQueryBuilder/DbSqlQueryBuilderBuildMethods.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,18 @@ private Expression BuildSelectWithExpression(MethodCallExpression expression)
626626
case ExpressionType.MemberAccess:
627627
if (selector.ReturnType.IsClass)
628628
{
629-
DbTableExpression dbTable = select.From.ToTableList().Single(x => x.Type.GenericTypeArguments[0] == selector.ReturnType);
629+
IEnumerable<DbTableExpression> tables = select.From.ToTableList();
630630

631-
columns = dbTable.Columns;
631+
if (tables.Any(x => x.Type.GenericTypeArguments[0] == selector.ReturnType))
632+
{
633+
DbTableExpression dbTable = tables.Single(x => x.Type.GenericTypeArguments[0] == selector.ReturnType);
634+
635+
columns = dbTable.Columns;
636+
}
637+
else
638+
{
639+
throw Error.InvalidOperation(SubSonicErrorMessages.MissingTableReferenceFor.Format(selector.ReturnType.Name));
640+
}
632641
}
633642
else
634643
{

SubSonic.Tests/DAL/SqlQueryProvider/SqlGeneratorTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace SubSonic.Tests.DAL.SqlQueryProvider
1616
using Linq;
1717
using Linq.Expressions;
1818
using Tests.DAL.SUT;
19+
using SubSonic.src;
1920

2021
[TestFixture]
2122
public partial class SqlQueryProviderTests
@@ -1093,6 +1094,22 @@ INNER JOIN page
10931094
query.Sql.Should().Be(expected);
10941095
}
10951096

1097+
[Test]
1098+
public void ThrowMissingReferenceForSelectStatementForIncludedEntityWhenMissingInclude()
1099+
{
1100+
FluentActions.Invoking(() =>
1101+
{
1102+
Person person = Context.People.First();
1103+
1104+
Expression select = person
1105+
.Renters
1106+
.Where((Renter) =>
1107+
DateTime.Today.Between(Renter.StartDate, Renter.EndDate.GetValueOrDefault(DateTime.Today.AddDays(1))))
1108+
.Select(x => x.Unit)
1109+
.Expression;
1110+
}).Should().Throw<InvalidOperationException>().WithMessage(SubSonicErrorMessages.MissingTableReferenceFor.Format(typeof(Unit).Name));
1111+
}
1112+
10961113
[Test]
10971114
public void CanGenerateSelectStatementForIncludedEntity()
10981115
{

0 commit comments

Comments
 (0)