views:

32

answers:

0

UPDATE: Probably should model my database to my objects correctly first! Found this tutorial http://www.codeproject.com/KB/linq/linqtutorial.aspx so should probably ignore this question!

Hi All,

Apologies if this already has a thread but I cant seem to find the answer I am looking for. I have an object Communication Type:

[Table(Name = "CommunicationTypes")]
public class CommunicationType
{
    [HiddenInput(DisplayValue = false)]
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int CommunicationTypeId { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableCommunicationTypesName")]
    [Required(ErrorMessage = "Please enter a Communication Type Name")]
    [Column] public string Name { get; set; }
}

Which I have mapped to my CommunicationTypes Table in SQL Server I also have a Problem object:

[Table(Name = "Problems")]
public class Problem
{
    [HiddenInput(DisplayValue = false)]
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int ProblemId { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TablePersonStudentName")]
    [Column] public int StudentId { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableCommunicationTypesName")]
    [Column] public int CommunicationTypeId { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemTypeName")]
    [Column] public int ProblemTypeId { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableMitigatingCircumstanceLevelName")]
    [Column] public int MitigatingCircumstanceLevelId { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemDate")]
    [Column] public DateTime? DateTime { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemOutline")]
    [Column] public string Outline { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemFile")]
    [Column] public byte[] MitigatingCircumstanceFile { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemAbsentFrom")]
    [Column] public DateTime? AbsentFrom { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemAbsentUntil")]
    [Column] public DateTime? AbsentUntil { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemRequestedFollowUp")]
    [Column] public DateTime? RequestedFollowUp { get; set; }

    public CommunicationType CommunicationType { get; set; }

    public MitigatingCircumstanceLevel MitigatingCircumstanceLevel { get; set; }

    public ProblemType ProblemCategory { get; set; }

    public ICollection<ProblemCommunication> ProblemCommunications { get; set; }

    public ICollection<AssessmentExtension> AssessmentExtensions { get; set; }

    public ICollection<User> Users { get; set; }

}

Which has-a Communication Type object. Upon a user attempting to delete a communication type if the communication type exists as a foreign key in the problems table the record is not deleted. If I try to delete the record in SQL Server it throws up an error. This is my repository code:

public void DeleteCommunicationType(CommunicationType communicationType)
{
    communicationTypesTable.DeleteOnSubmit(communicationType);
    communicationTypesTable.Context.SubmitChanges();
}

What I want to know is if the record is not deleted is there a way to throw an exception or something to notify my controller method. At the moment LINQ doesn't seem to care that SQL Server throws an error. As I am trying to use AJAX I have set up a throwJsonException action filter:

public class HandleJsonExceptionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAjaxRequest() && filterContext.Exception != null)
        {
            filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
            filterContext.Result = new JsonResult()
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                Data = new
                {
                    filterContext.Exception.Message,
                    filterContext.Exception.StackTrace
                }
            };
            filterContext.ExceptionHandled = true;
        }
    }       
}

Controller method:

 [HandleJsonException]
    public RedirectToRouteResult Delete(int communicationTypeId)
    {
        try
        {
            CommunicationType communicationType = sqlCommunicationTypeRepository.CommunicationTypes.First(c => c.CommunicationTypeId == communicationTypeId);
            sqlCommunicationTypeRepository.DeleteCommunicationType(communicationType);
            TempData["message"] = communicationType.Name + " was deleted.";
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            if(Request.IsAjaxRequest())
            {
                throw new Exception("An error has occurred in this method");
            }
            else
            {
                TempData["message"] = "Delete Operation Unsuccessful.";
                return RedirectToAction("Index");
            }
        }
    }