In EF 6.1.2 beta:
ToString() fails for all datatypes when dbContext.Configuration.UseDatabaseNullSemantics = true
(with message "The specified cast from a materialized 'System.xxx' type to the 'System.String' type is not valid")
The generated sql with UseDatabaseNullSemantics = true:
SELECT [Extent1].[Id] AS [Id] FROM [dbo].[Entities] AS [Extent1]
The generated sql with UseDatabaseNullSemantics = false
SELECT [Extent1].[Id] AS [Id], CAST( [Extent1].[Id] AS nvarchar(max))AS [C1]FROM [dbo].[Entities] AS [Extent1]
```
public class Entity
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Db : DbContext
{
public DbSet<Entity> Entities { get; set; }
}
static void Main(string[] args)
{
var db = new Db();
db.Configuration.UseDatabaseNullSemantics = true;
db.Entities.Add(new Entity { Name = "Test" + DateTime.Now });
db.SaveChanges();
var list = db.Entities.Select(d => new { Id = d.Id.ToString() }).ToList();
//fails with The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid.
}
```
ToString() fails for all datatypes when dbContext.Configuration.UseDatabaseNullSemantics = true
(with message "The specified cast from a materialized 'System.xxx' type to the 'System.String' type is not valid")
The generated sql with UseDatabaseNullSemantics = true:
SELECT [Extent1].[Id] AS [Id] FROM [dbo].[Entities] AS [Extent1]
The generated sql with UseDatabaseNullSemantics = false
SELECT [Extent1].[Id] AS [Id], CAST( [Extent1].[Id] AS nvarchar(max))AS [C1]FROM [dbo].[Entities] AS [Extent1]
```
public class Entity
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Db : DbContext
{
public DbSet<Entity> Entities { get; set; }
}
static void Main(string[] args)
{
var db = new Db();
db.Configuration.UseDatabaseNullSemantics = true;
db.Entities.Add(new Entity { Name = "Test" + DateTime.Now });
db.SaveChanges();
var list = db.Entities.Select(d => new { Id = d.Id.ToString() }).ToList();
//fails with The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid.
}
```