Cannot perform Inserts or Updates

Jun 18, 2014 at 12:18 AM
I have implemented an earlier version of the framework and it works great retrieving data. I have an issue though that it never saves a new insert and never saves an update to the database.

Kind of at a loss as to why it doesn't work as there are not any error messages being given at all. I have stack traced through the controller, to the services layer, into the repository and unit of work and it just goes right on through and never errors out, but it never commits the changes either. I can see the new and/or updated models all the way through as well.

I can see the proper data in the entity but it is never committed to the database - I am sure I am missing something simple or just being not overly familiar with this pattern as I've only used it a couple times and another dev set it up.

Code samples below:

controller attempting to add new record (does the Bind and gets the Asset Model where in I assign a new Guid per our existing system requirements being transferred to new system)
            if (ModelState.IsValid)
            {
                asset.id = Guid.NewGuid();
                _assetService.Add(asset);
                await _unitOfWork.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(asset);
Service implementation (other crud methods that work removed for brevity)
public class AssetService : Service<Asset>, IAssetService
    {
        private readonly IRepository<Asset> _repository;
        private readonly IRepositoryAsync<Asset_Address> _addressRepository;
        private readonly IRepositoryAsync<Portal_Users> _userRepository; 
        private readonly IUnitOfWork _unitOfWork;
        
        public Asset Add(Asset asset)
        {
            _repository.Insert(asset);
            return asset;
        }
}
Service interface
public interface IAssetService : IService<Asset>
    {
        IEnumerable<Asset> GetAsync();
        Task<Asset> FindAsync(Guid id);
        Asset Add(Asset asset);
        Asset Update(Asset asset);
        void Remove(Guid id);
    }
IRepository (as is from the framework)
namespace Repository.Pattern.Repositories
{
    public interface IRepository<TEntity> where TEntity : IObjectState
    {
        TEntity Find(params object[] keyValues);
        IQueryable<TEntity> SelectQuery(string query, params object[] parameters);
        void Insert(TEntity entity);
        void InsertRange(IEnumerable<TEntity> entities);
        void InsertGraph(TEntity entity);
        void InsertGraphRange(IEnumerable<TEntity> entities);
        void Update(TEntity entity);
        void Delete(object id);
        void Delete(TEntity entity);
        IQueryFluent<TEntity> Query(IQueryObject<TEntity> queryObject);
        IQueryFluent<TEntity> Query(Expression<Func<TEntity, bool>> query);
        IQueryFluent<TEntity> Query();
        IQueryable Queryable(ODataQueryOptions<TEntity> oDataQueryOptions);
        IQueryable<TEntity> Queryable();
        IRepository<T> GetRepository<T>() where T : IObjectState;
    }
}
Repository implementation (again stock as from the framework)
        public virtual void Insert(TEntity entity)
        {
            ((IObjectState) entity).ObjectState = ObjectState.Added;
            _dbSet.Attach(entity);
            _context.SyncObjectState(entity);
        }
Jun 25, 2014 at 6:50 AM
Updated to the 3.3 repo/UofW and same thing - I can get the entity all the way to the repo layer but the save is not committing the insert to the DB. Anyone have any ideas why? I am fairly new to the repo/UofW patterns and am likely missing something stupid simple.

Do I need to create an instance of my asset repo first for the UofW to insert a new item to? I don't know?
Coordinator
Jul 3, 2014 at 6:02 PM
Need to make sure your DbContext has singleton behavior for the lifecycle of httprequest, documented here using Unity.

https://genericunitofworkandrepositories.codeplex.com/wikipage?title=ASP.NET%20MVC%205%20Controller%20Example%20with%20Async&referringTitle=Documentation
Marked as answer by lelong37 on 7/3/2014 at 10:02 AM
Jul 3, 2014 at 7:25 PM
lelong37 wrote:
Need to make sure your DbContext has singleton behavior for the lifecycle of httprequest, documented here using Unity.

https://genericunitofworkandrepositories.codeplex.com/wikipage?title=ASP.NET%20MVC%205%20Controller%20Example%20with%20Async&referringTitle=Documentation
Thanks - I forgot to come back and update that I got it working.