-
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
NullReferenceException
when attempting to explicitly map a proxy type
#15958
Comments
@ibasin I have not been able to reproduce this--see my code below. Can you post a small, runnable project/solution or complete code listing (like below) that reproduces what you are seeing? public class Author
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public int AuthorId { get; set; }
public virtual Author Author { get; set; }
}
public class InMemoryDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLazyLoadingProxies()
.UseInMemoryDatabase("In-mem");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Author>();
}
}
public class SqliteDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLazyLoadingProxies()
.UseSqlite("Data Source = sqllitetest.db");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Author>();
}
}
public class Program
{
public static async Task Main()
{
using (var context = new InMemoryDbContext())
{
context.Database.EnsureCreated();
var ct = context.ChangeTracker;
context.Set<Author>().Add(new Author { Name = "Author1" });
await context.SaveChangesAsync();
}
using (var context = new SqliteDbContext())
{
context.Database.EnsureCreated();
var ct = context.ChangeTracker;
context.Set<Author>().Add(new Author { Name = "Author2" });
await context.SaveChangesAsync();
}
}
} |
Please see attached a simple solution that reproduces the problem. |
@ibasin This is happening because the code is attempting to register a dynamically generated proxy as an entity type: |
Thank you! This makes perfect sense. The second one picks up the dynamic proxies generated by the first one. Fixed ! |
Triage: better exception. |
NullReferenceException
when attempting to explicitly map a proxy type
Poaching. |
Attempting to create an application that connects to two databases seems to fail. For example, if I try to connect to SQLite and InMemory database at the same time (other combinations also fail), attempt to access ChangeTracker on the second DbContext results in a System.NullReferenceException: 'Object reference not set to an instance of an object.' This exception appears to happen in the ChangeTracker getter.
See the exception details below:
Steps to reproduce
The error is easy to reproduce.
Use the following code snippet to reproduce:
Both SqliteDbContext and InMemoryDbContext are plain vanilla. Just note that I have lazy loading turned on for both contexts.
Further technical details
EF Core version: 3.0.0-preview5.19227.1
Database Provider: Microsoft.EntityFrameworkCore.Sqlite & Microsoft.EntityFrameworkCore.InMemory
Operating system: Windows 10
IDE: Visual Studio 2019 Preview
The text was updated successfully, but these errors were encountered: