Search in Help for developer site.

Sunday 13 November 2016

Set Varchar over nvarchar globally in Entity Framework Code First Approach

Set Varchar over NVarchar globally in Entity Framework 6 Code First Approach

1. Overview

Hello to all my blog readers, Today i am gonna share something that is very useful for Entity Framework Code First Approach Lovers.After reading this article you will be able to set varchar data type and size in Code First Approach, So that when the Entity Framework generates the database it won't use nvarchar datatype.

2. Finding the Problem

lets assume that we have a Product Model class 

    public class tblProduct
    {
        [Key]
        public long ProductId { get; set; }
        public string ProductName { get; set; } 
    }


I 'm assuming that you have already set DbContext configuration according to your connivance.
Now when we fire update-database command in Package manager console then it creates the respective database and tables. By Default for string data types it converts to nvarchar data type in database table column. So here our requirement comes that we need varchar instead of nvarchar.

you must be wondering how to do that?

3. Solution

There are two method, Either you apply attribute to each property which has string data type or define it to Globally.

First Method:-

    public class tblProduct

    {
        [Key]
        public long ProductId { get; set; }
        [StringLength(100), Column(TypeName = "varchar")]

        public string ProductName { get; set; } 
        [StringLength(150), Column(TypeName = "varchar")]
        public string ProductDescription { get; set; }

 
    }

Using this method we have to mark each and every property which has string data type so that in database data type will be varchar.

Second Method:-

Define it Globally in your DbContext class. Add this method to your DbContext class.

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
            modelBuilder.Properties().Where(x => x.PropertyType.FullName.Equals("System.String") &&
                !x.GetCustomAttributes(false).OfType<ColumnAttribute>().Where(q => q.TypeName != null && q.TypeName.Equals("varchar", StringComparison.InvariantCultureIgnoreCase)).Any())
                .Configure(c =>
                c.HasColumnType("varchar").HasMaxLength(100));
  }

HasMaxLength(100) will define what should be default size.

Now in model class No need to mark with any attribute. it will take varchar(100) as a default data type for every string data type.

Here is our new model class
  public class tblProduct
    {
        [Key]
        public long ProductId { get; set; }
        [Required]
        public string ProductName { get; set; }
       
    }
Table Screenshot:-


 So by default data type will varchar and size would be 100.

4. Your Questions

I hope you liked my article , Please share it and post your question into the comment section if you have any.



2 comments:

  1. This was really helpful, Thank you for posting it! I personally prefer specifying the lengths of my string fields, so I removed the .HasMaxLength(100).

    ReplyDelete
  2. Thanks nab for your valuable feedback on Set Varchar over NVarchar globally in Entity Framework 6 Code First Approach

    ReplyDelete