title: Disable conversion between VARCHAR and NVARCHAR when using Entity Framework over an existing SQL server database
body: <p>I have an existing database SQL server and I'm using EF6 over it. Let's assume I have the following code:</p>
  
  <pre><code>private sealed class Foo
  {
      public int Id { get; set; }
  
      public string Bar { get; set; }
  }
  
  private sealed class FooConfiguration : EntityTypeConfiguration&lt;Foo&gt;
  {
      public FooConfiguration()
      {
          ToTable("Foos");
          HasKey(e =&gt; e.Id);
          Property(e =&gt; e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
      }
  }
  
  private class FooContext : DbContext
  {
      public FooContext(string connectionString)
          : base(connectionString)
      {
          Database.SetInitializer&lt;FooContext&gt;(null);
      }
  
      public virtual DbSet&lt;Foo&gt; Foos { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          modelBuilder.Configurations.Add(new FooConfiguration());
      }
  }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>const string bar = "حالياً البحث عبر جوجل";
  int fooId;
  using (var db = new FooContext(connectionString))
  {
      var foo = db.Foos.Add(new Foo { Bar = bar });
      db.SaveChanges();
      fooId = foo.Id;
  }
  
  using (var db = new FooContext(connectionString))
  {
      var foo = db.Foos.First(f =&gt; f.Id == fooId);
      if (foo.Bar != bar)
      {
          Console.WriteLine("Oops!");
      }
  }
  </code></pre>
  
  <p>What can possibly go wrong? Well, if the <code>Bar</code> column has type <code>VARCHAR(MAX)</code> instead of <code>NVARCHAR(MAX)</code>, then we are in a bad position, because <code>VARCHAR</code> cannot properly store that string of Unicode characters, so we get a bunch of question marks instead.</p>
  
  <p>So, the question is: can I some how disable this conversion between <code>VARCHAR</code> and <code>NVARCHAR</code> and enforce EF to throw some sort of type mismatch exception during <code>SaveChanges</code>? I've tried to use this in my configuration class:</p>
  
  <pre><code>Property(e =&gt; e.Bar).IsUnicode(true);
  Property(e =&gt; e.Bar).HasColumnType("NVARCHAR(MAX)");
  </code></pre>
  
  <p>but it did nothing.</p>
  
  <p>Thanks in advance.</p>