Search in Help for developer site.

Tuesday 29 November 2016

Difference between String and StringBulider

Description:- 

In this article I will explain difference between String and StringBuilder in C#.

String:-

String is immutable (Unchangeable). Immutable means once we create a String object we cannot modify it. Any operation like insert, append and replace to change string it will discard the old value and it will create a new instance in the memory to hold the new value.  

StringBulider:-

StringBuilder is mutable (Changeable). It means once we create StringBuilder object we can perform any operation like insert, append, and replace without creating new instance for every time.

Differences:- 

1. Performance wise String is slower than StringBuilder because every time string will create new instance.
2. String belongs to System namespace whereas StringBuilder belongs to System.Text namespace. 

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.