Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my unit of work codes:
public class UnitOfWork : IDisposable
    {
        
        Dent_DBEntities db = new Dent_DBEntities(PublicVariable.connectionString);
        private IPersonRepository _personRepository;
        public IPersonRepository PersonRepository 
        {
            get
            {
                if (_personRepository==null)
                {
                    _personRepository = new PersonRepository(db);
                }
                return _personRepository;
            }
        }

        private GenericRepository<Pay> _payRepository;

        public GenericRepository<Pay> PayRepository
        {
            get
            {
                if (_payRepository==null)
                {
                    _payRepository = new GenericRepository<Pay>(db);
                         
                }
                return _payRepository;
            }
        }
        private GenericRepository<SchTable> _schRepository;

        public GenericRepository<SchTable> SchRepository
        {
            get
            {
                if (_schRepository == null)
                {
                    _schRepository = new GenericRepository<SchTable>(db);

                }
                return _schRepository;
            }
        }

        private GenericRepository<ImageTable> _imageRepository;

        public GenericRepository<ImageTable> ImageRepository
        {
            get
            {
                if (_imageRepository == null)
                {
                    _imageRepository = new GenericRepository<ImageTable>(db);

                }
                return _imageRepository;
            }
        }

        private GenericRepository<PassTable> _passRepository;

        public GenericRepository<PassTable> PassRepository
        {
            get
            {
                if (_passRepository == null)
                {
                    _passRepository = new GenericRepository<PassTable>(db);

                }
                return _passRepository;
            }
        }
        public void Save()
        {
            db.SaveChanges();
        }
        public void Dispose()
        {
            db.Dispose();
        }
    }



& my generic Repository codes:
<pre>public class GenericRepository<TEntity>where TEntity:class
    {
        private Dent_DBEntities _db;
        private DbSet<TEntity> _dbSet;

        public GenericRepository(Dent_DBEntities db)
        {
            _db = db;
            _dbSet = _db.Set<TEntity>();
        }
        public virtual IEnumerable<TEntity>Get(Expression<Func<TEntity,bool>>where=null)
        {
            IQueryable<TEntity> query = _dbSet;
            if (where!=null)
            {
                query = query.Where(where);

            }
            return query.ToList();
        }
        public virtual void Insert(TEntity entity)
        {
            _dbSet.Add(entity);
        }

        public virtual TEntity GetById(Object Id)
        {
            return _dbSet.Find(Id);
        }

        public virtual void Update(TEntity entity)
        {


            _dbSet.Attach(entity);
            _db.Entry(entity).State = EntityState.Modified;
        }
        public virtual void Delete(TEntity entity)
        {
            if (_db.Entry(entity).State==EntityState.Detached)
            {
                _dbSet.Attach(entity);
            }
            _dbSet.Remove(entity);
        }

        public virtual void Delete(object Id)
        {
            var entity = GetById(Id);
            Delete(entity);
        }
    }


What I have tried:

how use connection string in generic repository and unit of work
Posted
Comments
[no name] 26-May-20 20:39pm    
Connection strings are typically handled in constructors (with some logic); why did you stick it in a property initializer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900