-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Projection throws NullReferenceException #16896
Comments
Verified that this works in the nightly builds. Test code: public class BloggingContext : DbContext
{
private readonly ILoggerFactory logger = LoggerFactory.Create(c => c.AddConsole());
public DbSet<Computer> Computers{ get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLoggerFactory(logger)
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
public class Program
{
public static async Task Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new Computer
{
ComputerName = "Rainbow",
PrimaryPerson = new Person
{
DisplayName = "Bungle"
},
Tenant = new Tenant
{
Name = "Zippy"
}
});
context.SaveChanges();
}
using (var context = new BloggingContext())
{
var results = context.Computers.Select(ComputerDto.Projection).ToList();
}
}
}
public class Computer
{
public int Id { get; set; }
public string ComputerName { get; set; }
public bool IsOnline { get; set; }
public bool IsPortable { get; set; }
public bool IsServer { get; set; }
public int RmmComputerId { get; set; }
public int RmmLinkId { get; set; }
public int RmmClientId { get; set; }
public string PrimaryUserDisplayName { get; set; }
public string PrimaryUserEmail { get; set; }
public string TenantName { get; set; }
public Person PrimaryPerson { get; set; }
public Tenant Tenant { get; set; }
}
public class Person
{
public int Id { get; set; }
public string DisplayName { get; set; }
public string EmailAddress { get; set; }
}
public class Tenant
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ComputerDto
{
public int Id { get; set; }
public string ComputerName { get; set; }
public bool IsOnline { get; set; }
public bool IsPortable { get; set; }
public bool IsServer { get; set; }
public int RmmComputerId { get; set; }
public int RmmLinkId { get; set; }
public int RmmClientId { get; set; }
public string PrimaryUserDisplayName { get; set; }
public string PrimaryUserEmail { get; set; }
public string TenantName { get; set; }
public static Expression<Func<Computer, ComputerDto>> Projection
{
get
{
return x => new ComputerDto()
{
Id = x.Id,
ComputerName = x.ComputerName,
IsOnline = x.IsOnline,
IsPortable = x.IsPortable,
IsServer = x.IsServer,
RmmComputerId = x.RmmComputerId,
RmmLinkId = x.RmmLinkId,
RmmClientId = x.RmmClientId,
PrimaryUserDisplayName = x.PrimaryPerson.DisplayName,
PrimaryUserEmail = x.PrimaryPerson.EmailAddress,
TenantName = x.Tenant.Name
};
}
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Similar from what I see at this issue #16724
Steps to reproduce
Further technical details
EF Core version: 3.0.0-preview7.19362.6
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2019
The text was updated successfully, but these errors were encountered: