Is it possible to clear a save-process (EF/AutoMappIng)?

51 Views Asked by At

I'm using EF/Automapping. An error occurs with a Foreign Key. With a try...catch, I want to catch the error. And I want to go further by inserting / updating data.

But I have the idea the process stays in the same exception. It looks the same as LINQ-to-SQL. That the SaveChanges-method try to save all updates who are waiting in the stack for saving.

Is it possible to clear a save-process?

The Insert-method in the LogMessagesService

public int Insert(LogBerichtDto LogBerichtDto)
{
    LogBericht entity = _mapper.Map<LogBerichtDto, LogBericht>(LogBerichtDto);

    _logBerichtRepository.Insert(entity);
    _logBerichtRepository.Save();

    return entity.Id;
}

In GenericRepository

public void Save()
{
    _context.SaveChanges();
}

enter image description here

1

There are 1 best solutions below

0
On

When I have time work on my own application, I can find out what's the problem? The were several problems. But the main problem I think the not saved changes stays in cache.

And the remark of Gert Arnold is the way I solved it.

This is my code of the GenericRepository. Not an Injection of the context anymore. And create new when needed. You can shoot. But the Import process is not hanging anymore.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace MyData.Repository
{
    public class GenericRepository<T> : IGenericRepository<T> where T : class
    {
        private MyDbContext _context;
        private DbSet<T> table = null;

        public GenericRepository()
        {
            _context = GetNew();
        }

        public DbContext Current
        {
            get { return _context; }
        }

        public virtual void Reset()
        {
            _context.Dispose();
            _context = GetNew();
        }

        public MyDbContext GetNew()
        {
            MyDbContext context = new MyDbContext();
            table = context.Set<T>();

            return context;
        }

        public IQueryable<T> Where(Expression<Func<T, bool>> filter)
        {
            return table.Where(filter);
        }

        public IEnumerable<T> GetAll()
        {
            return table.ToList();
        }

        public async Task<T> GetById(object id)
        {
            return await table.FindAsync(id);
        }

        public async Task Insert(T obj)
        {
            await table.AddAsync(obj);
        }

        public async Task Update(int id, T obj)
        {
            var entry = await table.FindAsync(id);
            _context.Entry(entry).CurrentValues.SetValues(obj);
        }

        public async Task Delete(int id)
        {
            T existing = await table.FindAsync(id);
            table.Remove(existing);
        }

        public async Task Save()
        {
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                Reset();
                throw;
            }

            _context.ChangeTracker.Clear();
        }
    }
}