context: namespace App.Dal
  {
      using Contracts;
      using Contracts.DataModels;
      using System;
      using System.Linq;
      using Contracts.Dals;
      using DataModels;
      using EntityDataModels;
      
      class QualificationDal : IDal<IQualificationDataModel>, IQualificationDal
      {        
          /// <summary> 
          /// Create a new Qualification
          /// </summary>
          public void Create(IQualificationDataModel item, IModelContext context)
          {            
              var ctx = new AppContext();
              var dataEntity = new QualificationEntityDataModel(item);
              ctx.Qualification.Add(dataEntity);
              ctx.SaveChanges();
              item.Id = dataEntity.Id;    
          }
          
          /// <summary> 
          /// Update a Qualification
          /// </summary>
          public void Update(IQualificationDataModel item, IModelContext context)
          {
              var ctx = new AppContext();
              var entity = ctx.Qualification.FirstOrDefault(x=> x.Id == item.Id);
              if (entity == null)
              {
                  return;
              }
              
  			ctx.Entry(entity).CurrentValues.SetValues(item);
              ctx.SaveChanges();
          }  
  
          /// <summary> 
          /// Delete an existing Qualification
          /// </summary>
          public void Delete(int id, IModelContext context)
          {
              var ctx = new AppContext();
              var itemToRemove = ctx.Qualification.SingleOrDefault(x => x.Id == id); 
  
              if (itemToRemove != null)
              {
                  ctx.Qualification.Remove(itemToRemove);
  				ctx.QualificationQualificationBody.RemoveRange(ctx.QualificationQualificationBody.Where(x => x.QualificationId == id)); // Remove any QualificationQualificationBody relationships 
  
  				ctx.Course.Where(x=>x.CourseQualificationId == id).ToList().ForEach(x=>x.CourseQualificationId = -1) ;// unset any CourseQualification relationships 
  
  				ctx.SaveChanges();
              }   
          }
          
          /// <summary> 
          /// Find a single Qualification
          /// </summary>
          public IQualificationDataModel Get(Func<IQualificationDataModel, bool> filter, IModelContext context)
          {
               var ctx = new AppContext();
               var rtn = ctx.Qualification.FirstOrDefault(filter);
               return rtn;
          }
          
          /// <summary> 
          /// Find a single Qualification
          /// </summary>
          public IQualificationDataModel Get(int id, IModelContext context)
          {
               var ctx = new AppContext();
               var rtn = ctx.Qualification.FirstOrDefault(x=>x.Id == id);
               return rtn;
          }
          
          /// <summary> 
          /// Find zero or more Qualification
          /// </summary>
          public IQueryable<IQualificationDataModel> GetAll(Func<IQualificationDataModel, bool> filter, IModelContext context)
          {
              var ctx = new AppContext();
              var rtn = ctx.Qualification.Where(filter).AsQueryable();
              return rtn;
          }
  
  		
  		#region 'CourseQualification'        
  		/// <summary>
          /// Gets the single Qualification by Course id for CourseQualification relationship.
          /// </summary>
          public IQualificationDataModel GetSingleQualificationByCourseForCourseQualification(int courseId, IModelContext context)
  		{
  			var ctx = new AppContext();
              var item = ctx.Course.Find(courseId);
  			if(item == null) return null;
  			var rtn = ctx.Qualification.Find(item.CourseQualificationId);
  			return rtn;
  		} 
  
  		/// <summary>
          /// Connects a Qualification to a Course for CourseQualification relationship.
          /// </summary>
          public void AddQualificationToCourseForCourseQualification(int qualificationId, int courseId, IModelContext context)
  		{
  			var ctx = new AppContext();
              var item = ctx.Course.Find(courseId);
              if (item == null) return;
              item.CourseQualificationId = qualificationId;
              ctx.SaveChanges();
  		}   
  
  		/// <summary>
          /// Unconnect a Qualification to a Course for CourseQualification relationship.
          /// </summary>
  		public void RemoveQualificationFromCourseForCourseQualification(int courseId, IModelContext context)
  		{
  			var ctx = new AppContext();
              var item = ctx.Course.Find(courseId);
              if (item == null) return;
              item.CourseQualificationId = -1;
              ctx.SaveChanges();
  		}  
  
  		#endregion 
  				
  		#region 'QualificationExam'
  		/// <summary>
          /// Returns all Qualification connected to a single Exam For the 'QualificationExam' relationship
          /// </summary>        
  		public IQueryable<IQualificationDataModel> GetAllQualificationByExamForQualificationExam(int examId, IModelContext context)
  		{
  			var ctx = new AppContext();
              var rtn = ctx.Qualification.Where(x => x.QualificationExamId == examId);
  			return rtn;
  		} 
  
  		/// <summary>
          /// Connects a Qualification to a Exam for QualificationExam relationship.
          /// </summary>
          public void AddQualificationToExamForQualificationExam(int qualificationId, int examId, IModelContext context)
  		{
  			var ctx = new AppContext();
              var item = ctx.Qualification.Find(qualificationId);
              if (item == null) return;
              item.QualificationExamId = examId;
              ctx.SaveChanges();
  		} 
  
  		/// <summary>
          /// Unconnects a Qualification to a Exam for QualificationExam relationship.
          /// </summary>
          public void RemoveQualificationFromExamForQualificationExam(int qualificationId, IModelContext context)
  		{
  			var ctx = new AppContext();
              var item = ctx.Qualification.Find(qualificationId);
              if (item == null) return;
              item.QualificationExamId = -1;
              ctx.SaveChanges();
  		} 
  		#endregion 
  		
  	
  		#region 'QualificationQualificationBody'		
  		// add child to parent ( QualificationBody Qualification )
  		/// <summary>
          /// Adds the qualificationbody to qualification for 'QualificationQualificationBody' relationship.
          /// </summary>
  		public void AddQualificationBodyToQualificationForQualificationQualificationBody(int qualificationbodyId, int qualificationId, IModelContext context)
  		{
  			var ctx = new AppContext();
              var existing = ctx.QualificationQualificationBody.Any(x=> x.QualificationBodyId == qualificationbodyId && x.QualificationId == qualificationId);
              if (existing) return;
              ctx.QualificationQualificationBody.Add(new QualificationQualificationBodyRelationshipModel{QualificationBodyId = qualificationbodyId, QualificationId = qualificationId});
              ctx.SaveChanges();
  		}
  
  		/// <summary>
          /// Removes the qualificationbody from qualification for 'QualificationQualificationBody' relationship.
          /// </summary>
  		public void RemoveQualificationBodyFromQualificationForQualificationQualificationBody(int qualificationbodyId, int qualificationId, IModelContext context)
  		{
  			var ctx = new AppContext();
              var existing = ctx.QualificationQualificationBody.FirstOrDefault(x=> x.QualificationBodyId == qualificationbodyId && x.QualificationId == qualificationId);
              if (existing == null) return;
              ctx.QualificationQualificationBody.Remove(existing);
              ctx.SaveChanges();
  		}
  
  		/// <summary>
          /// Get all Qualification for 'QualificationQualificationBody' relationship.
          /// </summary>
  		public IQueryable<IQualificationDataModel> GetAllForQualificationQualificationBody(int qualificationbodyId, IModelContext context)
  		{
  			var ctx = new AppContext();
  			var result = (from main in ctx.Qualification
  						join link in ctx.QualificationQualificationBody on main.Id equals link.QualificationId
  
gt: 						where (link.QualificationBodyId == qualificationbodyId)