Click here to Skip to main content
15,882,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have many to many relationship between doctor and clinic entities and also i am using unit of work generic Repository pattern. When i try to insert new doctor record i need to select multiple or single existing clinics. How can i insert doctor record with clinic reference and without duplication error.

DoctorManager.cs
****************
C#
public void InsertDoctor(Doctor doctor)
{
   //doctor object has clinics selected
   _unitOfWork.DoctorRepository.Insert(doctor);
   _unitOfWork.Save();
}


GenericRepository.cs

C#
public virtual void Insert(TEntity entity)
{
   _dbSet.Add(entity);
}


How can i implement _dbSet.Attach(); in generic repository to ovoid duplication error.
Could someone direct me with sample would be thankful.

C#
namespace DoctorBooking.DomainEntities
{
   public class Doctor
   {
     [Key]
     public Int32 DoctorId { get; set; }
     public string LastName { get; set; }
     public string FirstName { get; set; }

     public virtual ICollection Clinics { get; set; }

    }
}

C#
namespace DoctorBooking.DomainEntities
{
   public class Clinic
   {
     [Key]
     public Int32 ClinicId { get; set; }
     public string Name { get; set; }
     public int ContactNumber { get; set; }
     public string Email { get; set; }

     public virtual ICollection Doctors { get; set; }

   }
}
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