Search This Blog

Executing Stored Procedure with Entity Framework

The following is the description of executing stored procedure with Entity Framework.
It describes two separate methods for reading data and inserting/updating data through stored procedure.

The following is the code.

1. Fetching data

static List ExecuteReadStoredProcedure(string storedProcName, Dictionary parameters)
        {
            using (var entityContext = new ImplementedDbContext())
            {
                var storedProcedureParams = GetStoredProcedureParameters(storedProcName, parameters);
                return entityContext.Database.SqlQuery(storedProcedureParams.StoredProcedureName,
                    storedProcedureParams.Parameters.ToArray()).ToList();              
            }
        }
2. Insert/Update

static int ExecuteInsertUpdateStoredProcedure(string storedProcName, Dictionary parameters)
        {
            using (var entityContext = new ImplementedDbContext())
            {
                var storedProcedureParams = GetStoredProcedureParameters(storedProcName, parameters);
                return entityContext.Database.ExecuteSqlCommand(storedProcedureParams.StoredProcedureName,storedProcedureParams.Parameters.ToArray());
            }
        }

3. Helper method
private static StoredProcedureParams GetStoredProcedureParameters(string storedProcedureName,Dictionary parameters) {
            StringBuilder query = new StringBuilder();
            var listOfParameters = new List();

            query.Append(storedProcedureName + " ");
            var count = 0;

            foreach (var p in parameters)
            {
                count++;
                query.Append(p.Key);
                if (count != parameters.Count)
                {
                    query.Append(",");
                }

                listOfParameters.Add(new SqlParameter(p.Key, p.Value));
            }

            return new StoredProcedureParams() {
                StoredProcedureName = query.ToString(),
                Parameters = listOfParameters
            };
        }

 4. Object Parameter

 class StoredProcedureParams {
            public string StoredProcedureName { get; set; }
            public List Parameters { get; set; }
        }

5.  Implementing Read
var dictParameters = new Dictionary();
                dictParameters.Add("@parameter1",value of parameter 1);
                dictParameters.Add("@parameter2",value of parameter 2);

                var readResponse = ExecuteReadStoredProcedure("Your-Read-StoredProcedureName", dictParameters);
5.1 Response Object
public class MyResponse
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

6. Implementing Insert/Update
var dictParametersCreateEmailTemp = new Dictionary();
                dictParametersCreateEmailTemp.Add("@parameter1","value");
                dictParametersCreateEmailTemp.Add("@parameter2", "value");
                dictParametersCreateEmailTemp.Add("@parameter3", "value");
                dictParametersCreateEmailTemp.Add("@parameter4", "value");

                var insertResponse = ExecuteInsertUpdateStoredProcedure("Your-Insert-StoredProcedureName", dictParametersCreateEmailTemp);
                

Deploying MVC4 website in IIS in Windows Server 2008 R2

First step, publish the website, and then deploy it in IIS.
Error: Forbidden error, directory listing disabled
Solution: Add [module runAllManagedModuleForAllRequests=true]   to web.config under system.webServer node.

 [system.webServer]
    [validation validateIntegratedModeConfiguration="false" /]
[modules runAllManagedModulesForAllRequests="true" /]
........
[system.webServer]


Another option, is not to use this method , instead use
[modules]
  [remove name="UrlRoutingModule-4.0" /]
  [add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" /]
[/modules]

2. Error : Method not found: 'Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)'.

Cause:  using EF 5.0 with .net 4
Since, i was not using EF, i removed it with Nuget. As i created the website with the default Internet application template, it came along with.

3. Oracle Dataaccess inconsistencies
Solution: copied all the oracle odp.net dll to bin.

Turn On/Off Hypervisor from Command Prompt

The necessity of switching the hypervisor aroused when working with virtual box and windows phone.
The hypervisor needs to be turned on when working with projects related to windows phone and needs to be turned off, when working with virtual box or vmware.

For the following purposes, run the command prompt with administrative privileges.

1. Check the status of the hypervisor

Command: "bcdedit /enum
will list the current settings of the 'Windows Boot Manager' & 'Windows boot loader'. Under 'Windows Boot Loader ', check for the setting of 'hypervisorlaunchtype' . It will be 'Off' if it is turned off and 'Auto', if it's turned On.

2. Turn Off the hypervisor
Command: "bcdedit /set hypervisorlaunchtype off"
And restart the system. 

3. Turn On the hypervisor
Command: "bcdedit /set hypervisorlaunchtype auto"
And restart the system