Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling new DateTime() in projection causes NullReferenceException. #8608

Closed
dmitryshunkov opened this issue May 26, 2017 · 4 comments
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@dmitryshunkov
Copy link

dmitryshunkov commented May 26, 2017

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

@ajcvickers ajcvickers added this to the 2.0.0 milestone May 26, 2017
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 14, 2017
@dmitryshunkov
Copy link
Author

dmitryshunkov commented Jul 24, 2017

@smitpatel Still can reproduce with 2.0.0-preview2-final.

@smitpatel
Copy link
Member

@dmitryshunkov - As milestone says this was fixed in 2.0.0. So it is expected to reproduce in 2.0.0-preview2-final. Once we release 2.0.0 (which will be very soon), please verify on those packages.

@dmitryshunkov
Copy link
Author

Thanks, I was thinking it should be in preview, as fix was on dev branch.

@smitpatel
Copy link
Member

@dmitryshunkov - Fixes are (almost) always merged into dev branch. Based on release cycle, at certain point, we take snapshot of dev and generate release package out of it. For a fix to be in a certain release, it has to be merged to dev before we take snapshot. Generally milestone put on issues are better indicator for which release fix will be in. We update milestone based on when fix was merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants