-
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
Consider not emitting DetachedLazyLoadingWarning when entity is new rather than detached #19338
Comments
Note for triage: we could consider trying to change this. However, I expect setting new entity instances in the constructor is going to cause other problems. We should discuss this. |
Hi, Actually it was way too simplifed , so i put a more realistic code of the project. using Microsoft.EntityFrameworkCore;
using System.ComponentModel;
using System.Linq;
namespace EFCore3V2.BusinessLayer.Models
{
public class EFCore3V2Context : DbContext
{
public EFCore3V2Context()
{
var connectionString = "Data Source=TEST;" +
"Initial Catalog=EFCore3V2DB;Integrated Security=True;MultipleActiveResultSets=True";
DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(connectionString);
optionsBuilder.UseLazyLoadingProxies(true);
optionsBuilder.EnableDetailedErrors(true);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLazyLoadingProxies()
.UseSqlServer("Data Source=TEST;Initial Catalog=EFCore3V2DB;Integrated Security=True;MultipleActiveResultSets=True");
public DbSet<Invoice> Invoices { get; set; }
public DbSet<InvoiceDetail> InvoiceDetails { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Invoice>().HasKey(t => t.Id);
modelBuilder.Entity<InvoiceDetail>().HasKey(t => t.Id);
modelBuilder.Entity<InvoiceDetail>().HasOne(t => t.Invoice).WithMany(t => t.InvoiceDetails).HasForeignKey(t => t.InvoiceId).OnDelete(DeleteBehavior.Restrict);
}
public class Invoice
{
public int Id { get; set; }
public double Total { get; set; }
private EFCore3V2Context Context { get; set; }
public virtual BindingList<InvoiceDetail> InvoiceDetails { get; set; }
public Invoice()
{
InvoiceDetails = new BindingList<InvoiceDetail>();
InvoiceDetails.ListChanged += InvoiceDetails_ListChanged;
}
private void InvoiceDetails_ListChanged(object sender, ListChangedEventArgs e)
{
this.Total = InvoiceDetails.Sum(t => t.Price);
}
}
public class InvoiceDetail
{
public int Id { get; set; }
public int InvoiceId { get; set; }
public virtual Invoice Invoice { get; set; }
public string ProductCode { get; set; }
public double Price { get; set; }
}
}
} using EFCore3V2.BusinessLayer.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
namespace EFCore3V2.ConsoleUI
{
class Program
{
static void Main(string[] args)
{
EFCore3V2Context context = new EFCore3V2Context();
EFCore3V2Context.Invoice newInvoice = new EFCore3V2Context.Invoice();
EFCore3V2Context.InvoiceDetail newInvoiceDetail = new EFCore3V2Context.InvoiceDetail();
newInvoiceDetail.ProductCode = "AAA";
newInvoiceDetail.Price = 23;
newInvoice.InvoiceDetails.Add(newInvoiceDetail);
context.Invoices.Add(newInvoice);
context.SaveChanges();
EFCore3V2Context context2 = new EFCore3V2Context();
// Error Happens here when the object is loaded with Proxy
var data = context2.Invoices.ToList();
// Error Happens here when the object is loaded with Proxy
Console.ReadLine();
return;
}
}
} |
@vg2019 Just checking that you are aware that you can suppress this warning with something like: optionsBuilder.ConfigureWarnings(e => e.Ignore(CoreEventId.DetachedLazyLoadingWarning)) so this should not be blocking. |
Hi, @ajcvickers , Thank you for your comment.
This code + e.Ignore(CoreEventId.DetachedLazyLoadingWarning) , doesnt SHOW ** any errors( but in fact it cause an error but it is not caught or shown) , however once we try to access an Invoice , virtual properties( InvoiceDetails) can not be loaded anymore.
|
@ajcvickers hi , did you have the possibility to check again based on my last comment ? |
@vg2019 I am not able to reproduce this behavior with the code you have posted. Please post a small, runnable project or complete code listing that shows how lazy-loading is blocked. |
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it. BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions. |
Dear @ajcvickers , Please forgive me for very late reply, Program.cs
EFCore3V2Context.cs
I am not adding the Migration And ModelSnapshot as they are obviousAttached I am adding full project as .zip file. Looking forward hearing your comments. Thanks |
@vg2019 I downloaded and ran the attached project, but nothing fails for me. Is there something I need to do to make this fail? |
Dear @ajcvickers Hi, I upload a little bit modified ( removed irrelevant lines) version of the project here. https://www.youtube.com/watch?v=sLIRE3cUgQY&feature=youtu.be |
@vg2019 Thanks; I was now able to reproduce this. Two things:
To fix this second issue, don't call the virtual property from the constructor. For example: public virtual BindingList<InvoiceDetail> InvoiceDetails { get; }
public Invoice()
{
var bindingList = new BindingList<InvoiceDetail>();
bindingList.ListChanged += InvoiceDetails_ListChanged;
InvoiceDetails = bindingList;
} |
This no longer repros on 7.0. |
We are using EF Core 3.0
Below is a very simplified sample of our code, to reproduce the error
On our application , when we load an Account from database , with optionsBuilder.UseLazyLoadingProxies(true) , if we write any code in the Constructor part that tries to access a navigation property ( AccountAddress for the current error case) , we receive the following error, however the object is neither detached nor the context is disposed.
It is understandable that , the Proxy is creating an instance of our Class and overriding the virtual properties, so at initalization stage , AccountAddress is null, however, before in EF 6.3 , the behaviour of proxy was to show the property ( AccountAddress ) as null, and not show an error.
Error
Steps to reproduce
EFCore3V2Context.cs
Program.cs
Further technical details
EF Core version: EF Core 3.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.3
The text was updated successfully, but these errors were encountered: