title: How to Validate against Entity Framework Core Field Properties with Fluent Validation
body: <p>I'm trying to validate against the .HasMaxLength property from Entity Framework Core.</p>
  <p>Here is a sample model builder method:</p>
  <pre><code>        protected override void OnModelCreating(ModelBuilder modelBuilder)
               {
                modelBuilder.Entity&lt;Address&gt;(entity =&gt;
                   {
                
                    entity.Property(e =&gt; e.AddressEmail)
                  .IsRequired()
                  .HasMaxLength(255)
                  .IsUnicode(false)
                  .HasDefaultValueSql(&quot;('')&quot;);
  </code></pre>
  <p>Using my context, I can find the maximum length like this:</p>
  <pre><code>  dbContext.Model.FindEntityType(typeof(Address)).FindProperty(&quot;AddressEmail&quot;).GetMaxLength()
  </code></pre>
  <p>Using Fluent Validation, I can do this:</p>
  <pre><code>  RuleFor(Address =&gt; Address.AddressEmail).NotNull().MaximumLength(255);
  </code></pre>
  <p>So that works but, when I change the database structure, I have to update my validators.</p>
  <p>I'm trying to figure out how to drive my Fluent Validation rules from the entity framework MaxLength property</p>
  <p>Any help would be greatly appreciated.</p>
  <p>**** Partially working way of bringing the dbcontext in but I still am struggling getting the rule:</p>
  <pre><code> public AddressValidator(DbContext dbContext)
        {
          Microsoft.EntityFrameworkCore.Metadata.IEntityType et = dbContext.Model.FindEntityType(typeof(Address));
  
          foreach (var Property in et.GetProperties())
          {
              var maxLength = Property.GetMaxLength();
    ----&gt;&gt;&gt;&gt;&gt; RuleFor(x =&gt;x.{PropertyName})    Invalid but this is the idea
          }
              
  </code></pre>