I'm using EF 6 in my project. All i want is to fetch some data from the database and sometimes join additional data.
Here is my code:
var queryable = dbContext.Manuals
.AsNoTracking()
.Where(x => x.Status == "ACTIVE");
if (showDisabledParents)
{
var parentsIds = queryable
.Where(x => x.ParentId.HasValue)
.Select(x => x.ParentId.Value)
.Distinct()
.ToArray();
queryable = queryable.Union(
dbContext.Manuals
.AsNoTracking()
.Where(x => parentsIds.Contains(x.Id))
);
}
It works well when showDisabledParents is false. EF will generate a SQL query with correct column names.
But when showDisabledParents is true then EF generates UNION statement(which is expected) but it uses C1, C2, C3 ... for column names.
And the problem is that i have a custom DbDataReader which calls DateTime.SpecifyKind(..., DateTimeKind.Utc) if column name ends with "Utc". And since EF is using wrong column names (C1, C2, C3, ...) my logic is not working.
Is it somehow possible to prevent this behavior? I mean if there is a way to tell EF to not use these weird column names.
UPDATED: Here is my DBDataReaderCode:
public class MyDbDataReader : DelegatingDbDataReader
{
private string _dbName;
public MyDbDataReader(string dbName, DbDataReader source)
: base(source)
{
_dbName = dbName;
}
public override DateTime GetDateTime(int ordinal)
{
return DateTime.SpecifyKind(base.GetDateTime(ordinal), base.GetName(ordinal).EndsWith("UTC", StringComparison.OrdinalIgnoreCase)
? DateTimeKind.Utc
: DateTimeKind.Local);
}
}