Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting last inserted row id using repository pattern

I'm new to this repository pattern. I have the following methods in repository.

public abstract class Repository<T> : IRepository<T> where T : class
    {
        private PHOnlineEntities dataContext;
        private readonly IDbSet<T> dbset;

        protected Repository(IDatabaseFactory databaseFactory)
        {
            DatabaseFactory = databaseFactory;
            dbset = DataContext.Set<T>();
        }

        protected IDatabaseFactory DatabaseFactory
        {
            get;
            private set;
        }

        protected PHOnlineEntities DataContext
        {
            get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
        }

        public virtual int Add(T entity)
        {
            dbset.Add(entity);
            dataContext.SaveChanges();

            // return id here
        }

        public virtual void Update(T entity)
        {
            dbset.Attach(entity);
            dataContext.Entry(entity).State = EntityState.Modified;
        }


        public virtual void Delete(T entity)
        {
            dbset.Remove(entity);
        }

        public virtual void Delete(Expression<Func<T, bool>> where)
        {
            IEnumerable<T> objects = dbset.Where<T>(where).AsEnumerable();
            foreach (T obj in objects)
                dbset.Remove(obj);
        }

        public virtual T GetById(long id)
        {
            return dbset.Find(id);
        }

        public virtual T GetById(string id)
        {
            return dbset.Find(id);
        }

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

        public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
        {
            return dbset.Where(where).ToList();
        }

        public T Get(Expression<Func<T, bool>> where)
        {
            return dbset.Where(where).FirstOrDefault<T>();
        }

This is my CustomerRepository class

    public interface ICustomerDetailRepository : IRepository<CustomerDetail>
    {
    }

    /// <summary>
    /// CustomerDetail repository
    /// </summary>
    public class CustomerDetailRepository : Repository<CustomerDetail>, ICustomerDetailRepository
    {
        /// <summary>
        /// 
        /// </summary>
        private PHOnlineEntities _dataContext;

        /// <summary>
        /// 
        /// </summary>
        protected IDatabaseFactory DatabaseFactory
        {
            get;
            private set;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="databaseFactory"></param>
        public CustomerDetailRepository(IDatabaseFactory databaseFactory)
            : base(databaseFactory)
        {
            DatabaseFactory = databaseFactory;
        }

        /// <summary>
        /// 
        /// </summary>
        protected PHOnlineEntities DataContext
        {
            get { return _dataContext ?? (_dataContext = DatabaseFactory.Get()); }
        }
    }

CustomerDetail class contains Model. It has all the Entity column along with ID column.

When i Add the entity to database, i want to return last inserted row id. The Id is identity column. Could anyone help me on this?

like image 790
PaRsH Avatar asked Jan 13 '23 21:01

PaRsH


2 Answers

Check your CustomerDetail object after inserting, the ID will be populated already

public class CustomerDetail
{
    public int Id{ get; set; }
    public string Name{ get; set; }
    public string Address{ get; set; } 
}
var customerDetail = new CustomerDetail { Name = "Bubbles", Address = "1 way, city" }
customerDetailRepository.Add(customerDetail)

Console.WriteLine(customerDetail.Id); // This is the identity value
like image 186
sunil Avatar answered Jan 15 '23 11:01

sunil


You have to create an interface like that:

public interface IEntity
{
    public int Id { get; set;}
}

Make your entities implement that interface and change your repository class:

public abstract class Repository<T> : IRepository<T> where T : class, IEntity
{

    (...)

    public virtual int Add(T entity)
    {
        dbset.Add(entity);
        dataContext.SaveChanges();

        // return id here
        return entity.Id;
    }

}
like image 36
MarcinJuraszek Avatar answered Jan 15 '23 11:01

MarcinJuraszek