Skip to content
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

problem with EF Core Lazy load Proxy #125

Open
luizfbicalho opened this issue Oct 18, 2022 · 0 comments
Open

problem with EF Core Lazy load Proxy #125

luizfbicalho opened this issue Oct 18, 2022 · 0 comments

Comments

@luizfbicalho
Copy link

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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant