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 !