none
Validation Attribute blocking update query RRS feed

  • Question

  • Hello In my App I'm using EF and  I declared my own validationAttribute:

    public class UniqueDeviceInstanceId : ValidationAttribute
        {
            public override bool IsValid(object value)
            {
                if(value==null)
                {
                    return false;
                }
                var No = int.Parse(value.ToString());
                var a = db.DeviceUsages.Where(model => model.DeviceInstanceId == No).FirstOrDefault();
                if (a != null)
                {
                    return false;
                }
                return true;
            }
        }

    Its used in this class:

    [MetadataType(typeof(DeviceUsage))]
    public partial class DeviceUsage
    {
        [Required]
        public int StorageId { get; set; }
        public int UserId { get; set; }
        [Required]
        [UniqueDeviceInstanceId]
        public int DeviceInstanceId { get; set; }
    }

    But when I'm trying to call this function with update query. Validation throws voliation on the unique attribute. Even I'm not touching it.

    public ActionResult Edit([Bind(Include="StorageId,UserId,DeviceInstanceId")] DeviceUsage deviceusage)
        {
            if (ModelState.IsValid)
            {
                db.Entry(deviceusage).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DeviceInstanceId = new SelectList(db.DeviceInstances, "Id", "SerialNo", deviceusage.DeviceInstanceId);
            ViewBag.StorageId = new SelectList(db.Storages, "Id", "Name", deviceusage.StorageId);
            ViewBag.UserId = new SelectList(db.Users, "Id", "EmployeeNo", deviceusage.UserId);
            return View(deviceusage);
        }

    How to modify my code to make it work? At this moment and can't update this table.

    Tuesday, January 28, 2014 8:46 AM

Answers

  • Hello,

    >>But when I'm trying to call this function with update query. Validation throws voliation on the unique attribute. Even I'm not touching it.

    In Entity Framework, after we set:

    db.Entry(deviceusage).State = EntityState.Modified;

    It means we have touched all properties of the entity. For example, if the DeviceInstanceId is 1 and after we call the line code above, it means we change it from 1 to 1, although it is strange. Since the state is modified, the validation is triggered. And in actual the entity whose DeviceInstanceId is 1 already exists in database.

    So remove this line:

    db.Entry(deviceusage).State = EntityState.Modified;

    Or do exact changes in DeviceInstanceId property like change its value from 1 to 2.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 29, 2014 2:09 AM
    Moderator