I have the following setup:
private IQueryable<ManagementCreditorEntity> CreateQuery(ExternalId creditorId)
{
//_creditorManagedPropertyDbContext is of type ICreditorManagedPropertyDbContext
return _creditorManagedPropertyDbContext.ManagementCreditorEntities
.Where(entity => entity.Creditor.ExternalId == creditorId);
}
public interface ICreditorManagedPropertyDbContext
{
IDbSet<ManagementCreditorEntity> ManagementCreditorEntities { get; }
}
public class CreditorEntity
{
public short CompanyId { get; set; }
public int CreditorId { get; set; }
public Guid ExternalId { get; set; }
}
public class ManagementCreditorEntity
{
public short CompanyId { get; set; }
public int CreditorId { get; set; }
public CreditorEntity Creditor { get; set; }
}
public sealed class ExternalId
{
private readonly Guid _value;
public ExternalId(Guid id)
{
_value = id;
}
public static implicit operator Guid(ExternalId id)
=> id?._value ?? Guid.Empty;
}
I'm calling the above using:
var query = CreateQuery(new ExternalId(Guid.NewGuid()));
This results in the the following SQL query:
-- With ExternalId as the param type for CreateQuery(), note the "OR (1 = 0)" at the end
SELECT
[Extent1].[CompanyID] AS [CompanyID],
[Extent1].[CreditorID] AS [CreditorID],
FROM [PropertyManagement].[ManagementCreditor] AS [Extent1]
INNER JOIN [PropertyManagement].[Creditor] AS [Extent2] ON ([Extent1].[CreditorID] = [Extent2].[CreditorID]) AND ([Extent1].[CompanyID] = [Extent2].[CompanyID])
WHERE ([Extent2].[ExternalID] = @p__linq__0) OR (1 = 0)
I noticed that if I change the param type of CreateQuery() from ExternalId to Guid, the query is the same except the redundant OR (1 = 0) is removed.
-- With Guid as the param type for CreateQuery()
SELECT
[Extent1].[CompanyID] AS [CompanyID],
[Extent1].[CreditorID] AS [CreditorID],
FROM [PropertyManagement].[ManagementCreditor] AS [Extent1]
INNER JOIN [PropertyManagement].[Creditor] AS [Extent2] ON ([Extent1].[CreditorID] = [Extent2].[CreditorID]) AND ([Extent1].[CompanyID] = [Extent2].[CompanyID])
WHERE [Extent2].[ExternalID] = @p__linq__0
Why does OR (1 = 0) get added if I wrap a Guid in a reference type (ExternalId)?
I'm using .NET 4.6.2 and Entity Framework 6.2.0.