Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am having problems saving data when many to many relation is involved. I am using Repository pattern in Entity Framework Code First approach.

Sample code is as follows:

C#
public partial class Employee : BaseEntity
    {
        private List<Role> _roles;

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public virtual List<Role> Roles
        {
            get { return _roles ?? _roles= new List<Role>()); }
             set { _roles= value; }
        }
    }

public partial class Employee : BaseEntity
    {
        private List<Employee> _employees;

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public virtual List<Employee> Employees
        {
            get { return _employees ?? _employees = new List<Employee>()); }
            set { _employees = value; }
        }
    }


The values are populated and sent to the WCF service for updation. In our case Employees and Roles already exist and they are being mapped against each other in junction table.

C#
public partial interface IRepository<T> where T : BaseEntity
    {
        T GetById(object id);
        void Insert(T entity);
        void Update(T entity);
    }
/*-----------------------------------*/

   public partial class EfRepository<T> : IRepository<T> where T : BaseEntity
    {
        private readonly IDbContext _context;
        private IDbSet<T> _entities;

        public EfRepository(IDbContext context)
        {
            this._context = context;
        }

        public T GetById(object id)
        {
            return this.Entities.Find(id);
        }

    public void Insert(T entity)
        {
            try
            {
                if (entity == null)
                    throw new ArgumentNullException("entity");

                this.Entities.Add(entity);

                this._context.SaveChanges();
            }
            catch (Exception ex)
            {
            }
        }

    public void Update(T entity)
    {
            try
            {
                if (entity == null)
                    throw new ArgumentNullException("entity");

                this._context.SaveChanges();
            }
            catch (Exception ex)
            {
            }
        }
}
/*-----------------------------------*/

public partial class EmployeeService : IEmployeeService
{
        private readonly IRepository<Employee> _employeeRepository;

    public PermissionService(IRepository<Employee> employeeRepository)
        {
            this._employeeRepository = employeeRepository;
        }

    public virtual void UpdatePermissionRecord(Employee employee)
        {
            _employeeRepository.Update(employee);
        }

}


I can see the added Roles in employee object in function UpdatePermissionRecord but when the line is executed there are no changes in database.

Initially updation of a simple table wouldn't work.

C#
public partial class EmployeeTest : BaseEntity
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string Address { get; set; }
        
    }


On adding Automapper and changing the Update code to

Solution 1 -

C#
public virtual void UpdatePermissionRecord(EmployeeTest employeeTest)
        {
        AutoMapper.Mapper.CreateMap<EmployeeTest, EmployeeTest>();

            var updateEmployeeTest = AutoMapper.Mapper.Map<EmployeeTest, EmployeeTest>(employeeTest);

            _employeeTestRepository.Update(updateEmployeeTest);
        }


Solution 2 -

C#
        public void ChangeState<T>(T entity, EntityState state) where T : class
        {
            if (base.Entry(entity).State == EntityState.Detached)
                base.Entry(entity).State = state;

        }
/*------------------------------*/
	public void Update(T entity)
	{
            try
            {
                if (entity == null)
                    throw new ArgumentNullException("entity");

		_context.ChangeState(entity, EntityState.Modified);
                                  
                this._context.SaveChanges();
            }
            catch (Exception ex)
            {
            }
        }


Solution 1 & 2 work for EmployeeTest (Single Class Updation) but do not work for Employee (Many to Many Updation).

Please suggest a way to save values in many to many case using WCF entity framerwork repository pattern.
Posted

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