Namotion.Reflection
Namotion.Reflection copied to clipboard
problem with EF Core Lazy load Proxy
I created this classes
[Table("Left")]
public class Left
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
[DisallowNull]
public virtual Child Child { get; set; } = null!;
public virtual Left? Next { get; set; }
public virtual IList<Right> Rights { get; set; } = new List<Right>();
}
[Table("Child")]
public class Child
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
}
[Table("Right")]
public class Right
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
public virtual IList<Left> Lefts { get; set; } = new List<Left>();
}
And have this problem
var left1 = new Left();
left1.EnsureValidNullability();
This throw the error
If I get from the database I get no error
var optionsBuilder = new DbContextOptionsBuilder<ModelContext>().UseLazyLoadingProxies()
.UseSqlServer("Server=.\\sql2019;Database=Nullable;Trusted_Connection=false;user id=sa;pwd=sa;MultipleActiveResultSets=true;Encrypt=false");
using (var context = new ModelContext(optionsBuilder.Options))
{
var left = context.Left.Find(leftid);
if (left != null)
{
var x = left.Child;
left.GetType()?.GetProperty("Child")?.SetValue(left, null);
x = left.Child;
left.EnsureValidNullability();
}
}