Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

How to update record using Entity Framework?

$
0
0

HI

I am using generic repository pattern for CRUD operations. I want to update the record and I am using the below code 

 public interface IRepository<TEntity> : IDisposable
    {
        List<TEntity> GetAll();
        TEntity GetSinglEntity(Func<TEntity, bool> predicate);
        bool Delete(TEntity entity);
        bool Add(TEntity entity);
        bool Update(TEntity entity);
    }
 public class DataRepository<TEntity> : IRepository<TEntity> where TEntity : class
{

 public bool Update(TEntity entity)
        {
            try
            {
                using (xrateEntities entities = new xrateEntities())
                {
                    entities.Entry(entity).State = System.Data.Entity.EntityState.Modified;
                    // Below code is adding the new record
                    entities.Set<TEntity>().Add(entity); // How to update the entity or record
                    entities.SaveChanges();
                    return true;
                    // log here
                }
            }
            catch (Exception exception)
            {
                // log here
                throw exception;
            }
            return false;
        }

}

How to update the record using Entity framework?
Thanks


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>