Newtonsoft.Json.JsonSerializationException:在 'Castle.Proxies.SalonProxy' 从 'MyEntity' 获取值时出错

Newtonsoft.Json.JsonSerializationException: Error getting value from 'MyEntity' on 'Castle.Proxies.SalonProxy'

大家好,我有 Asp.Net Core Web API 项目使用 Entity Framework Core 6.0 和 Postgresql 作为数据库

我配置了我的实体及其关系。一切都很好,但只有通用存储库设计模式有问题

如果我只使用 context.Products.ToList(),它工作得很好。但是每当我将它与通用存储库设计模式一起使用时。我得到一个错误。但是有一件很有趣的事情。每当我使用断点仅 10 秒或更长时间并且我在 10 秒后保持断点时,都没有错误。一切都很完美。是不是异步问题,我没用过?

错误 enter image description here

Newtonsoft.Json.JsonSerializationException: Error getting value from 'Seats' on 'Castle.Proxies.SalonProxy'.

---> System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.LazyLoadOnDisposedContextWarning': An attempt was made to lazy-load navigation 'Seats.SalonProxy' after the associated DbContext was disposed. This exception can be suppressed or logged by passing event ID 'CoreEventId.LazyLoadOnDisposedContextWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.

public class EfEntityRepositoryBase<TEntity, TContext> : IEntityRepository<TEntity>
where TEntity : class, IEntity, new()
where TContext : DbContext, new()
{
    public TEntity Add(TEntity entity)
    {
        using (TContext context = new TContext())
        {
            var addedEntity = context.Entry(entity);
            addedEntity.State = EntityState.Added;
            context.SaveChanges();
            return addedEntity.Entity;
        }
    }

    

    public void Delete(TEntity entity)
    {
        using (TContext context = new TContext())
        {
            var deletedEntity = context.Entry(entity);
            deletedEntity.State = EntityState.Deleted;
            context.SaveChanges();
        }
    }

    public TEntity Get(Expression<Func<TEntity, bool>> filter)
    {
        using (TContext context = new TContext())
        {
            var result = context.Set<TEntity>().SingleOrDefault(filter);
            return result;
        }
    }

    public List<TEntity> GetAll(Expression<Func<TEntity, bool>> filter = null)
    {
        using (TContext context = new TContext())
        {
    
         var result = filter == null
          ? context.Set<TEntity>().ToList()
          : context.Set<TEntity>().Where(filter).ToList();
          return result;
        }
    }
}

}

我的沙龙实体

public class Salon : IEntity
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public string SalonNumber { get; set; }

    public virtual List<Seat> Seats { get; set; }

    public virtual List<Ticket> Tickets { get; set; }
}

我的座位实体

    public class Seat : IEntity
    {
        [Key]
        public int Id { get; set; }
    
        public int SeatNumber { get; set; }
    
        public int SalonId { get; set; }
        public virtual Salon Salon { get; set; }
    }

 

MyDbContext

public class AppDbContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseLazyLoadingProxies().ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.DetachedLazyLoadingWarning))
            .UseNpgsql("Server=localhost;Database=BiletsgoDB;Port=5432;Username=postgres;Password=123456");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Ticket>().HasOne(t => t.Category).WithMany(b => b.Tickets).HasForeignKey(t => t.CategoryId);
            modelBuilder.Entity<Ticket>().HasOne(t => t.Salon).WithMany(b => b.Tickets).HasForeignKey(t => t.SalonId);
            modelBuilder.Entity<Seat>().HasOne(t => t.Salon).WithMany(b => b.Seats).HasForeignKey(t => t.SalonId);
            modelBuilder.Entity<TicketFile>().HasOne(t => t.Ticket).WithMany(b => b.TicketFiles).HasForeignKey(t => t.TicketId);
        }

        public DbSet<Ticket> Tickets { get; set; }
        public DbSet<TicketFile> TicketFiles { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Seat> Seats { get; set; }
        public DbSet<Salon> Salons { get; set; }
    }

我解决了问题

参数包括我们的实体关系

 public List<TEntity> GetAll(params Expression<Func<TEntity, object>>[] including)
    {
        using (TContext context = new TContext())
        {

            var include = context.Set<TEntity>().AsQueryable();
            including.ToList().ForEach(item =>
            {
                include = include.Include(item);
            });
            return include.ToList();
        }
    }

我运行这个方法来自我的业务层

_salonDal.GetAll(t => t.Seats,t => t.Tickets)

但是我意识到这种方式效率不高。通用存储库基础应该使用基础实体。这是我的看法