[英]Newtonsoft.Json.JsonSerializationException: Error getting value from 'MyEntity' on 'Castle.Proxies.SalonProxy'
Hi everyone, I have Asp.Net Core Web API project that is using Entity Framework Core 6.0 and Postgresql as a database大家好,我有使用 Entity Framework Core 6.0 和 Postgresql 作为数据库的 Asp.Net Core Web API 项目
I configured my entities and their relation.我配置了我的实体及其关系。 Everything is fine, but there is only problem in Generic Repository Design Pattern
一切都很好,但是通用存储库设计模式中唯一的问题
If I use just context.Products.ToList()
, it is working perfect.如果我只使用
context.Products.ToList()
,它工作得很好。 But whenever I am using it with Generic Repository Design Pattern.但是每当我将它与通用存储库设计模式一起使用时。 I got an error.
我有一个错误。 But there is an interesting thing.
但是有一个有趣的事情。 Whenever I use break point just 10 seconds or even more and I keep it up after 10 seconds, there is no error.
每当我只使用 10 秒甚至更长时间的断点并在 10 秒后保持它,就没有错误。 Everything is working perfect.
一切都很完美。 Is there Asynchronous problem, I never used it ?
是否有异步问题,我从未使用过它?
Error enter image description here在此处输入图像描述时出错
Newtonsoft.Json.JsonSerializationException: Error getting value from 'Seats' on 'Castle.Proxies.SalonProxy'.
Newtonsoft.Json.JsonSerializationException:从“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.
---> System.InvalidOperationException:为警告“Microsoft.EntityFrameworkCore.Infrastructure.LazyLoadOnDisposedContextWarning”生成错误:在处置关联的 DbContext 后尝试延迟加载导航“Seats.SalonProxy”。 This exception can be suppressed or logged by passing event ID 'CoreEventId.LazyLoadOnDisposedContextWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
通过将事件 ID 'CoreEventId.LazyLoadOnDisposedContextWarning' 传递给 'DbContext.OnConfiguring' 或 'AddDbContext' 中的 'ConfigureWarnings' 方法,可以抑制或记录此异常。
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;
}
}
}
} }
My Salon Entity我的沙龙实体
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; }
}
My Seat Entity我的座位实体
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 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; }
}
I Solved the problem我解决了问题
params includes our entity relations params 包括我们的实体关系
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();
}
}
And I run this method from my business layer我从我的业务层运行这个方法
_salonDal.GetAll(t => t.Seats,t => t.Tickets)
But I realized this way it is not efficient.但我意识到这种方式效率不高。 Generic Repository base should use fundamentals entities.
通用存储库基础应使用基础实体。 That's my opinion
这是我的意见
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.