Daniel Vega
Daniel Vega

Reputation: 578

In memory database disposed by the initial use

I'm trying to use In-Memory Database. I can read from it, but if I try to add something it throws an exception of type System.ObjectDisposedException.I cant really spot differences with working examples around. I'm using EF Core, .net core 3.1. Also, I'm using it along with a repository pattern where currently I have a single repository. Startup :

 services.AddDbContext<WeirdContext>(options =>
 {
    options.UseInMemoryDatabase("WeirdDb")
           .EnableSensitiveDataLogging()
           .ConfigureWarnings(b => b.Ignore(InMemoryEventId.TransactionIgnoredWarning));                
 });

 services.AddTransient<IMyOnlyRepository, MyOnlyRepository>();

repository:

 private readonly WeirdContext _context;

 public ShowtimesRepository(WeirdContext context)
 {
     _context = context;
 }

 public ShowtimeEntity Add(WeirdEntity weirdEntity)
 {
     context.Showtimes.Add(showtimeEntity);
     var id =_context.SaveChanges();
     return GetCollection(ent => ent.Id == id).FirstOrDefault();
 }
...

how it's being consumed in a service:

public async void Create(WeirdModel weirdModel)
{
    var extraDataModel = await imdbService.fetchSomestuff(weirdModel.Id);
    weirdModel.extraData = mapper.Map<extraData>(extraDataModel); // mapping goes well
    var weirdEntity = mapper.Map<WeirdEntity>(weirdModel);
    WeirdRepository.Add(showtimeEntity);
 }

And the controller

[HttpPost]
public IActionResult Post([FromBody] WeirdModel weirdModel)
{
    showtimeService.Create(weirdModel);
    return StatusCode(201); 
}

Can someone spot what's off?

Upvotes: 0

Views: 202

Answers (0)

Related Questions