Using Azure SQL Database with EF Code First

  • Open the existing ASP.NET MVC Web Application or Create a New Application.
  • Go to Tools => NuGet Package Manager => Manage Nuget Packages for Solution
  • Add reference to EntityFramework
  • To the ASP.NET MVC Web Application add the following in Employee.cs under Models folder.
public class Employee
    {
        [Key]
        public int Id { get; set; }
        public string EmpName { get; set; }
        public decimal Salary { get; set; }
    }
    public class DemoOrganization : DbContext
    {
        public DbSet<Employee> Employees { get; set; }
    }
  • In Web.Config add the following
<connectionStrings>
    <add name="DemoOrganization" connectionString="Data Source=.\sqlexpress;Integrated Security=True;database=DemoOrganization" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  • Right Click on Controller Folder => Add => Controller…=> Select “MVC 5 Controller with Views, using Entity Framework” => Add
  • Select Model = Employee, DataContext Class = “DemoOrganziation” => Add

Note: This generates Controller along with Views for Add/Edit/Delete operations.

  • Go to Tools => NuGet Package Manager => Package Manager Console => At prompt Execute Commands, Enable-Migrations
  • Open the Migrations\Configuration.cs file and add the following to Seed method.
context.Employees.AddOrUpdate(
  p => p.Id,
  new Employee  { EmpName = "E1", Salary=1000 },
  new Employee { EmpName = "E2" ,Salary=2000},
  new Employee { EmpName = "E3" ,Salary = 3000}
);
  • In the Package Manager Console enter the command:

                 Add-Migration “Initialize”

                  Update-Database

  • Run the application locally.

Publish the App to Azure:

  • Azure Portal => Create Sql Server and Database
  • Azure Portal => Create a New WebApp
  • In Visual Studio => right-click the project in Solution Explorer and select Publish from the context menu
  • Under Profile Tab => Click on Microsoft Azure App Service
  • Click on New Button, Go to Services Tab => Click on SQL Database + => Create a New Database => Provide a Database Name, Administrator Username, and Password => OK

OR

  • Under Settings Tab => Provide Database Details and Check “User this connection string at runtime (update destination web.config)” and Also check => Execute Code First Migrations (runs at application start)
  • Publish the Web App and Note the progress in Azure App Service Activity Window
  • Test the application by visiting http://<DemoApp>.azurewebsites.net/Employees.

Part 2:

  • Add “Age” column to the Employee class
  • Delete EmployeesController and corresponding views.
  • Recreate EmployeesController as in step 6 and 7.
  • In the Package Manager Console enter the command:

Add-Migration “AddAgeColumn”

Update-Database

  • Publish the Application and ensure that in Settings, ensure that “Code First Migration…” A checkbox is checked.
  • Test the application by visiting http://<DemoApp>.azurewebsites.net/Employees.

SandeepSoni.jpg

Mr. Sandeep Soni, CEO & Founder of Deccansoft Software Services.He has over 21years of Experience in Teaching and Development using Microsoft Technologies. Since 1997, of the total 100000+ students Deccansoft has trained, he has personally trained over 60,000+ students and we are proud of him because almost every student he had trained is very happy with the quality and many are well placed in various I.T Firms.

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *