title: Unable to validate entity using Entity Framework Core (.net core)
body: <p>I am using EntityFramework Core to connect to database in .Net core web API. I am facing issue while trying validate the entity before submit changes to DB i.e Context.SaveChanges().  Validation error is not thrown even though entity is not meeting specific condition. 
  If i add the condition using attribute it work, but it is not working when conditions are added using Model build.</p>
  
  <p>Is there way to properly validate entity which are created using Model builder?</p>
  
  <p>Entity:</p>
  
  <pre><code>public class AppSession
  {
      public AppSession()
      {
  
      }
  
      public long AppSessionId { get; set; }
      public Guid ExternalAppSessionId { get; set; }
      public DateTime CreatedDate { get; set; }
      public Sources SourceId { get; set; }
      public string Gcid { get; set; }
      //[StringLength(60)]
      public string UserAgent { get; set; }
      public string SessionReferrer { get; set; }      
  
  }
  </code></pre>
  
  <p>Model Builder:</p>
  
  <pre><code>public class AppSessionConfig : IEntityTypeConfiguration&lt;AppSession&gt;
      {
          public void Configure(EntityTypeBuilder&lt;AppSession&gt; builder)
          {
              builder.Property(e =&gt; e.AppSessionId).HasColumnName("AppSessionID");
  
              builder.Property(e =&gt; e.ExternalAppSessionId).HasColumnName("ExternalAppSessionID");
  
              builder.Property(e =&gt; e.Gcid)
                   .HasColumnName("GCID")
                   .HasMaxLength(200)
                   .IsUnicode(false);
  
  
              builder.Property(e =&gt; e.Keyword)
                   .HasMaxLength(255)
                   .IsUnicode(false);
  
              builder.Property(e =&gt; e.SessionReferrer)
                   .HasMaxLength(512)
                   .IsUnicode(false);
  
              builder.Property(e =&gt; e.SourceId).HasColumnName("SourceID");
  
              builder.Property(e =&gt; e.UserAgent)
                   .HasMaxLength(50)
                   .IsUnicode(false);
  
          }
  </code></pre>
  
  <p>Context:</p>
  
  <pre><code>public partial class TestDbContext : DbContext,IDbContext
  {
      public TestDbContext(DbContextOptions&lt;TestDbContext&gt; options)
     : base(options)
      { }
  
      public virtual DbSet&lt;Entities.AppSession&gt; AppSessions { get; set; }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.ApplyConfiguration(new AppSessionConfig());
     }
  
      public override int SaveChanges()
      {
          ValidateEntities();
          return base.SaveChanges();
      }
  
  private void ValidateEntities()
      {
          var entities = (from entry in ChangeTracker.Entries()
                          where entry.State == EntityState.Modified || entry.State == EntityState.Added
                          select entry.Entity);
  
          var validationResults = new List&lt;ValidationResult&gt;();
          foreach (var entity in entities)
          {
              var validationContext = new ValidationContext(entity);
              Validator.ValidateObject(entity, validationContext, true);
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Thanks,</p>