1

good day ..

i created a model that has an property with [Notmapped] DataAnnotations and i created another class inherit from this model with same property but i add required DataAnnotations the problem is when i delete i got error "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

"

My Model :

        [Key]
    [Display(AutoGenerateField = true, AutoGenerateFilter = true, Description = "IDDescription", GroupName = "IDGroupName", Name = "IDName", ShortName = "IDShortName", Prompt = "IDPrompt", Order = 50, ResourceType = typeof(Resources.BaseEntity))]
    public long ID { get; set; }


    [StringLength(207, ErrorMessageResourceName = "StringTooMuch", ErrorMessageResourceType = typeof(Resources.BaseSlider))]
    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "DetailsRequired", ErrorMessageResourceType = typeof(Resources.BaseSlider))]
    [Display(Name = "Description", ResourceType = typeof(Resources.BaseSlider))]
    public string Description { get; set; }

    [NotMapped]
    public string ShortDescription
    {
        get
        {
            if (Description.Length <= 207)
            {
                return Description;
            }
            return Description.Substring(0, 207);
        }
    }

    [Display(Name = "HasBTN", ResourceType = typeof(Resources.BaseSlider))]
    public bool HasBTN { get; set; }

    [Display(Name = "Is Image Dark")]
    public bool IsDark { get; set; }

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

    [Display(Name ="Slider Type")]
    public long SliderTypeID { get; set; }


    [NotMapped]
    //[ImageValidation(".jpg,.png,.japg", OriginalWidth = 1920, OriginalHeight = 600)]
    [Display(AutoGenerateField = true, AutoGenerateFilter = true, Description = "ImagePathDescription", Name = "ImagePathName", ResourceType = typeof(Resources.BaseMore))]
    public virtual HttpPostedFileBase ImagePathFile { get; set; }

    #endregion

    #region Relations

    public virtual IList<BaseSliderPhotoUpload> Photos { get; set; }

    public virtual BaseLookup SliderType { get; set; }

    #endregion

    public BaseSlider()
    {
        Photos = new List<BaseSliderPhotoUpload>();
    }

and the class i created :

public class BaseSliderCreate : BaseSlider
{

    #region Data


    [NotMapped]
    [Required]
    //[ImageValidation(".jpg,.png,.japg", OriginalWidth = 1920, OriginalHeight = 600)]
    [Display(AutoGenerateField = true, AutoGenerateFilter = true, Description = "ImagePathDescription", Name = "ImagePathName", ResourceType = typeof(Resources.BaseMore))]
    public override HttpPostedFileBase ImagePathFile { get; set; }

    #endregion

}

in delete actionresult code :

  public ActionResult DeleteConfirmed(Guid id)
    {
        BaseSlider SliderObject = db.Sliders.Where(x => x.GUID == id && x.Deleted == null).FirstOrDefault();
        SliderObject.Deleted = DateTime.Now;
        SliderObject.DeletedByID = _CurrentUser.ID;

        // Delete All Photos
        DeletePhoto DeletePhoto = new DeletePhoto();



        var DeletedPhotoName = new List<string>();
        foreach (var name in SliderObject.Photos)
        {
            DeletedPhotoName.Add(name.FileName);
        }

        if (DeletePhoto.PhotoDeleted("Slider", DeletedPhotoName))
        {
            try
            {
                db.SliderPhotos.RemoveRange(SliderObject.Photos);
                db.Entry(SliderObject).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ErrorList.Add(ex.Message);
                throw;
            }
        }
        else
        {
            ErrorList.Add(DeletePhoto.ErrorMessage);
        }

        ViewBag.ErrorList = ErrorList;



        return RedirectToAction("Delete", new { id = SliderObject.GUID });
    }

when i save change i got error

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

i checked i found the EntityValidationErrors is that ImagePathFile is required..

thanks for helping my and i apologist for my bad English

kareem khalil
  • 29
  • 2
  • 8
  • Your editing data, so use a view model and remove your `[NotMapped]` properties from your data models - [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Oct 16 '17 at 21:56
  • i tried this solution but it gives me error "The entity type SliderViewModel is not part of the model for the current context."@StephenMuecke – kareem khalil Oct 16 '17 at 22:28
  • A view model has no association with EF (it goes in a separate folder - say `ViewModels`), and you did not show what code you used so its impossible to tell what you did wrong. –  Oct 16 '17 at 23:31
  • Also, which line is the error on? – aaron Oct 17 '17 at 00:44

0 Answers0