Entity framework 4.3 with required association

后端 未结 3 1866
南旧
南旧 2021-01-13 07:14

I\'m a very strange behavior with EF code first approach and associations. I have two entities:

public class GlobalKpiSectionn
{
    public GlobalKpiSection(         


        
3条回答
  •  情话喂你
    2021-01-13 07:53

    EF disabled lazy loading when it validates the entities. It does this to avoid unnecessary round trips to database due to the validations placed on navigational properties.

    Model the scalar property in your entity and place the validation attribute there.

    public class Region
    {
        public int RegionId { get; set; }
    
        public bool IsMain { get; set; }
    
        [Required]
        public int? KpiSectionId { get; set; }
    
        public virtual GlobalKpiSection KpiSection { get; set; }
    }
    

提交回复
热议问题