1

I want to update banner without changing AuthorId, and my DTO does not include AuthorId so that field got null value when I submit.

I have followed this post Exclude Property on Update in Entity Framework

My code:

public void UpdateBanner(Banner banner)
{
    _context.Entry(banner).State = EntityState.Modified;
    _context.Entry(banner).Property(x => x.AuthorId).IsModified = false;

    try
    {
        _context.SaveChanges();
    }
    catch (DbEntityValidationException e)
    {
        foreach (var eve in e.EntityValidationErrors)
        {
            Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);

            foreach (var ve in eve.ValidationErrors)
            {
                Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                ve.PropertyName, ve.ErrorMessage);
            }
        }

        throw;
    }         
}

And I get this error:

The AuthorId field is required.

Please help !

Community
  • 1
  • 1
Kaeszt
  • 50
  • 5

1 Answers1

0

The error is self explanatory.

The AuthorId field is required. It seems to me by looking at the link that you posted as reference, you are still using your primary key field to mark it as false for Modified attribute whereas in the link, a field which was not a primary key was used.

So you might want to check that difference in the fields and if its a primary key, then it will demand a value.

Hope this helps.

Matt Murdock
  • 3,203
  • 6
  • 20
  • 34
  • Tthanks for answer me, the AuthorId (not a primary key) was added, but what i want is keeping the old value. So i dont want to include AuthorId in bannerDTO (object to submit when update), that's why AuthorId is null. – Kaeszt Dec 02 '15 at 10:16
  • @Kaeszt - Check the DB for this field, if its marked as `not null`. – Matt Murdock Dec 02 '15 at 10:19
  • I know it it required, but my need is to keep the current value of it, not to update new. – Kaeszt Dec 02 '15 at 10:23
  • Then in that case, you will have to pass it as it is. Because a required field will always expect a value, even if its the same value. – Matt Murdock Dec 02 '15 at 10:25
  • thanks for help, but i still need a better solution, becoz this will +1 query to get the authorId, and the logic is not really updating – Kaeszt Dec 02 '15 at 10:33
  • Isn't it received in the `banner` object ? – Matt Murdock Dec 02 '15 at 10:38
  • yes, it's received for sure, my edit view allow to edit on some specified field, so i pass that fields by a bannerDTO, and the other fields of banner model are null, which i want to keep the old value of – Kaeszt Dec 02 '15 at 10:42
  • Well then you might as well pass `AuthorId` to that view and that would solve this. Since its a required field, it would have to maintained during DB transactions. – Matt Murdock Dec 02 '15 at 10:45
  • Let me explain: - 2 reasons why authorId not inlcuded in bannerDTO: + 1st i want to keep old value + 2nd - i dont want to show authorId on view - i dont want to query the current authorId value (performance purpose), so if i must pass it, i still set it as null – Kaeszt Dec 02 '15 at 10:50
  • 2 things here. First, if you dont want to show, you can make use of a Hidden Field and have access to the value. Second, check once in the `Banner` model and your EF model, if you have the Required attribute anywhere. – Matt Murdock Dec 02 '15 at 10:59
  • seem like you don't get my question, thanks for helping – Kaeszt Dec 02 '15 at 11:03