The following code worked fine with EF Core 2.2, but no longer with EF Core 3:
``` C#
var wrappedEntities = context.Products
.Select(p => new ProductWrapper(p));
The exception text reads:
> System.InvalidOperationException: 'When called from 'VisitMemberInit', rewriting a node of type 'System.Linq.Expressions.NewExpression' must return a non-null value of the same type. Alternatively, override 'VisitMemberInit' and change it to not visit children of this type.'
Using a parameterless constructor and passing the entity via a public property works:
``` C#
var wrappedEntities = context.Products
.Select(p => new ProductWrapper
{
Entity = p
});
Is this a bug or an intended breaking change: Linq queries are no longer evaluated on the client
EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10 1903
IDE: Visual Studio 2019 16.3.9
@mnissl I have not been able to reproduce this--see my code below. Please post a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing.
```C#
public class Product
{
public int Id { get; set; }
}
public class ProductWrapper
{
public ProductWrapper(Product product)
{
}
}
public class BloggingContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
public DbSet<Product> Products { get; set; }
}
public class Program
{
public static async Task Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.AddRange(new Product(), new Product());
context.SaveChanges();
}
using (var context = new BloggingContext())
{
var wrappedEntities = context.Products.Select(p => new ProductWrapper(p));
}
}
}
```
I could reproduce the issue: It doesn't work if you use both a parameterized constructor and an (empty) property initialization list.
public class Product
{
public int Id { get; set; }
}
public class ProductWrapper
{
public string Name { get; set; }
public ProductWrapper()
{
}
public ProductWrapper(Product product)
{
}
}
public class BloggingContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
public DbSet<Product> Products { get; set; }
}
public class Program
{
public static void Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.AddRange(new Product(), new Product());
context.SaveChanges();
}
using (var context = new BloggingContext())
{
// This works!
context.Products.Select(p => new ProductWrapper()).ToList();
// This works!
context.Products.Select(p => new ProductWrapper { }).ToList();
// This works!
context.Products.Select(p => new ProductWrapper { Name = "ABC" }).ToList();
// This works!
context.Products.Select(p => new ProductWrapper(p)).ToList();
// This will throw in Debug, but in Release only with attached debugger!?!
context.Products.Select(p => new ProductWrapper(p) { }).ToList();
// This will throw in Debug and Release!
context.Products.Select(p => new ProductWrapper(p) { Name = "ABC" }).ToList();
}
}
}
Hello,
Great spot ! It's a quite annoying bug when you develop with on a Clean Architecture/DDD way.
When you think this will be available ?
I have two project ( one new & one migrations ready ) that have this issue, and I waste some time to analyse when the issue come from me or not.
Thanks for the answer
@smitpatel Would it be possible to have this fix available in EF Core 3.0.x or 3.1? Milestone 5.0.0 sounds like .NET 5 sounds like November 2020!?
Seriously 5.0.0? Since this is broken between 2.2 and 3.0 it would most appreciated if this would make it to 3.1.0.
I've stumbled upon this issue as well while converting from 2.2 to 3.0. The fix should come ASAP.
Note from triage: we will consider patching this for a 3.1.x release.
Most helpful comment
Note from triage: we will consider patching this for a 3.1.x release.