Closed
Description
Calling default DateTime constructor in select method causes NullReferenceException. Need it for example, to convert TimeSpan to DateTime.
Exception message: Object reference not set to an instance of an object.
Stack trace:
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode[T](IList`1 expressions)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
at Microsoft.EntityFrameworkCore.Query.CompiledQueryCacheKeyGenerator.CompiledQueryCacheKey.GetHashCode()
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalCompiledQueryCacheKeyGenerator.RelationalCompiledQueryCacheKey.GetHashCode()
at Microsoft.EntityFrameworkCore.Query.Internal.SqlServerCompiledQueryCacheKeyGenerator.SqlServerCompiledQueryCacheKey.GetHashCode()
at System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)
at System.Collections.Concurrent.ConcurrentDictionary`2.TryAdd(TKey key, TValue value)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Remotion.Linq.QueryableBase`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at TestEF.Program.Main() in C:\Git\TestEF\TestEF\Program.cs:line 62
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace TestEF
{
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
//=> optionsBuilder.UseInMemoryDatabase();
=> optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=WonkyVB;Trusted_Connection=True;");
}
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime DateTime { get; set; }
public int? BlogId { get; set; }
public Blog Blog { get; set; }
}
public class PostDTO
{
public DateTime DateTime { get; set; }
public string Title { get; set; }
}
public class Program
{
public static void Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new Blog
{
Posts = new List<Post> { new Post { Title = "First" }, new Post { Title = "Second" }, new Post { Title = "Third" } }
});
context.SaveChanges();
}
using (var context = new BloggingContext())
{
Func<DateTime> getDate = () => new DateTime();
var list = context.Posts.Select(p => new PostDTO
{
DateTime = new DateTime(), // Produces NullReferenceException
//DateTime = getDate() // Works
//DateTime = new DateTime(1, 1, 1) // Works
}).ToList();
foreach (var post in list)
{
Console.WriteLine(post.DateTime);
}
}
}
}
}
Further technical details
EF Core version: 1.1.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017