title: EF Core - Specify characters that has to be within string for saving to succeed
body: <p>Working on one of my own spare time project, using Entity Framework Core to persist data, i would like to configure, via Fluent Api, that a property of type string, must contain a selection of character for it to be valid when saving.</p>
  
  <p>Given a User as so...</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class User : IUser
      {
          public int Id { get; set; }
          public byte[] Version { get; set; }
          public string Username { get; set; }
          public string Password { get; set; }
          public string Email { get; set; }
  
          ...
      }
  </code></pre>
  
  <p>And configuring with Fluent Api like this...</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class UserConfig : EntityConfig&lt;User&gt;
      {
          public override void Configure(EntityTypeBuilder&lt;User&gt; builder)
          {
              base.Configure(builder);
  
              ...
  
              builder.HasIndex(x =&gt; x.Username).IsUnique();
              builder.Property(x =&gt; x.Username).HasMaxLength(30);
          }
      }
  </code></pre>
  
  <p>How would i create an Extension method for PropertyBuilder of type string, that Entity Framework Core can understand? So that i could do something along these lines...</p>
  
  <pre class="lang-cs prettyprint-override"><code>builder.Property(x =&gt; x.Email).MustContain('@', '.');
  </code></pre>
  
  <p>The Extension method would look something like the code below, it is the logic within it, that i am unsure of how to construct.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static class PropertyBuilderExtensions
      {
          public static PropertyBuilder&lt;string&gt; MustContain(this PropertyBuilder&lt;string&gt; builder, params char[] chars)
          {
              // ???
          }
      }
  </code></pre>
  
  <p>It would allow the possibility to validate that for example an Email contains the required @ and . characters, which is always within a valid Email, when trying to save it to the database.
  This check could also be done manually when creating a user, but i still fell like it would be a good thing, if the database simply denied saving it, if it didn't contain the specified characters.</p>
  
  <p>Fell free to ask questions if there is need for any further explanation.</p>