Modifying a property on an entity in Entity Framework causes validation error

前端 未结 2 1929
生来不讨喜
生来不讨喜 2021-01-23 06:04

I am trying to simply load an entity, modify a property and then save it back to the database.

var db = new NewsletterContext();
var newsletter  = db.Newsletter         


        
2条回答
  •  隐瞒了意图╮
    2021-01-23 06:24

    Entity framework will disable lazy loading when doing the validation. Hence if you put required validation on navigational properties the validation will fail. You can decorate the scalar property related to the navigational property instead.

    public class Foo
    {
    
        [Required]
        public int? RequiredScalarId { get; set; }
    
        public virtual Bar RequiredNavigationalProp { get; set; }
    }
    

提交回复
热议问题