Thursday, January 5, 2017

How to set identity specification set to false globally in entity framework

We can globally turn off this feature by removing StoreGeneratedIdentityKeyConvention Convention as below,

public class OurContext : DbContext 
protected override void OnModelCreating(DbModelBuilder modBuilder) {        modBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>();    

Thursday, December 22, 2016

microsoft visual studio can not set breakpoint in c# file

Microsoft visual studio can not set breakpoint in c# file

I was getting same error in one of my project, and after 15 to 20 mins of search & practical i found very simple solution as below,

BUILD > Clean Solution

BUILD > Build Solution

Hope it will help you guys.

Saturday, November 12, 2016

WCF InvalidOperationException: A binding instance has already been associated to listen URI

Here you missed the address attribute in your metadata endpoint.

A binding instance has already been associated to listen URI

Without it WCF thinks that you want to host the mex endpoint at the same address as other endpoints.

Sunday, June 26, 2016

Cannot insert the value NULL into column in ASP.NET MVC Entity Framework

Cannot insert the value NULL into column in ASP.NET MVC Entity Framework

One of my friend was trying simple MVC application [CRUD application] CRUD means 
C - Create, R - Read, U - Update & D- delete 

and he was getting error like "Cannot insert the value NULL into column in ASP.NET MVC Entity Framework" from almost more than half hour as below,

entity framework null value issue for id key

Here he was trying to save id & name into department table where id was not primary key of table.
Although he was specifying id value explicitly he was getting above error, because entity framework by default considers id as primary key and does not supply values to sql server.


    public class Department
        public int id { get; set; }

        public string Name { get; set; }


Here you just need to add one line above id property, to say entity framework that do not consider as database generated value.

If we want to set this option globally for every entities (for all tables), then we can do this by two ways as below,

- By setting identity specification set to false globally

  How to set identity specification set to false globally

- By creating custom conventions & add that custom conventions to DBContext class

  How to set DatabaseGeneratedOption Globally for all entities

- Using Fluent API

public class MyContext : DbContext {
    protected override void OnModelCreating(DbModelBuilder modBuilder) {
                    .Property(e => e.Id)

Monday, June 13, 2016

Delegates in C# with examples

Delegate is type safe function pointer.

Function pointer: points/holds referernce to a method.

Type safe:  return type and signature of method must be same as signature and return type of method else you will get compilation error.

Syntax:  Very much similar to the method with the delegate keyword.

It is similar to a class, we need to create instance of it and pass in the function name to the delegate constructor and this is the function the delegate will point to.

It calls the method indirectly via a pointer.

It is representative to communicate between two parties.

When you want to pass function as parameter to another funtion than think of delegates.

Main use of delegates in c# is Callbacks & communication between two parties.


Example1:  Suppose we have report class and in that we have download report method. At real time you want to know the percentage of report downloaded, so in that case we can make use of delegates as below,

class Program
        static void Main(string[] args)
       // holds reference to function.

        // callback function which you want to call using delegates.
        static void ReportDownloadStatus(int i)

    public class Report
        //declare delegate
        public delegate void PercentageCompleted(int i);
        // delegate passed as a function parameter
        public static void DownloadReport(PercentageCompleted percentageCompleted) 
            // Gathering and printing data
            for (int i = 0; i <= 100; i++)

            Console.WriteLine("Report Downloaded completly.");

Example2: Calling content page method from master page method / Click events.


             protected void Button1_Click(object sender, EventArgs e)
                 if (contentCallEvent != null)
                   contentCallEvent(this, EventArgs.Empty);

            public event EventHandler contentCallEvent;


protected void Page_PreInit(object sender, EventArgs e)
               // Create an event handler for the master page's contentCallEvent event
               Master.contentCallEvent += new EventHandler(PrintData);

           private void PrintData(object sender, EventArgs e)
              // This Method will be Called.
              lbl.Text = "Delegates";


<%@ MasterType VirtualPath="~/Site.Master" %>

Dear Reader, please give suggestion on it, or let me know if you have good examples for delegates.

Saturday, June 11, 2016

.Net Framework interview questions and answers part2

6. What is Common Language Specification (CLS)?

This is a subset of the CTS, which all .NET languages are expected to support. It was always a dream of Microsoft to unite all different languages in to one umbrella and CLS is one-step towards that. Microsoft has defined CLS, which are nothing but guidelines that language should follow so that it can communicate with other .NET languages in a seamless manner.

7. What is an IL or MSIL or CIL?

Intermediate Language (IL) is also known as MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate Language). 
All .NET source code is compiled to IL. 
IL is then converted to machine code or at run-time by a Just-In-Time (JIT) compiler.

8. What is Garbage collection / Memory Management in .Net?

Garbage collection is automatic process performs by Garbage collector (which lives in CLR) to free up memory of objects which are no longer in used.

Every time you create a new object, the common language runtime allocates memory to store that object. But at some point that object may be no longer needed by your application. So that garbage collector deallocates memory of this type of objects, so that application can store new objects into memory. If memory is not deallocated then sooner your application will run out of memory.

Interview Questions Related to garbage collections

a. How does Garbage collection works?

Every time a garbage collection is performed, the garbage collector basically looks at the memory and frees up memory which is no longer needed, that is, memory which is occupied by ‘dead objects’.

b.  How does it know when an object is ‘dead’?

An object is dead if it is unreachable by your code. The obvious example is a local variable inside a method. There is no way for your code to access that variable once the method has returned, so it becomes ‘dead’.

C.  How often does the garbage collector perform a garbage collection?

There are three ways a garbage collection can be triggered. 
Firstly, if your system has low physical memory, this can trigger a garbage collection.
Secondly, a threshold is defined which indicates an acceptable level of memory on the heap which can be used by allocated objects. If this threshold is surpassed, then a garbage collection is triggered.
Finally, a garbage collection can explicitly be triggered by calling the GC.Collect method. Only under very rare circumstances is this ever required.

Sunday, May 29, 2016

PermCheck Codility Solution in C#.Net

PermCheck Codility Solution in C#.Net 

Check whether array A is a permutation.


public static int PermCheck(int[] A)
            int l = A.Length;
            int Result = 1;
            if (l == 0)
                return Result = 1;
                for (int i = 0; i < l; i++)
                    if (A[i] != (i + 1))
                        Result = 0;
                return Result;
And Here goes the Result, Got 100%

Perm Check