Purpose:
Steps by steps to build a ASP.NET MVC project with code first and publish to Azure
Azure Portal
Create Resource Group
*Login Azure portal, and Select “Resource groups” on the side barCreate Database
*Login Azure portal, and Select “SQL databases” on the side bar -> click “Add”
*If you selected the one who has already serve a free database, it will
show some error message like the following picture.
*Set the firewall for database server
1. Click “Set server firewall”
2. Add the client IP address to this rule
Connect DB to ASP.NET MVC Project
*Create ASP.NET MVC project
*Install Entity Framework
- Tools -> NuGet Package Manager -> Manage NuGet Package for Solution…
* *Add connection string
a. Right click project -> Properties -> Settings
b. Select “Connection string” Type and click the right extend button
a. Right click project -> Properties -> Settings
b. Select “Connection string” Type and click the right extend button
*Create Model
a. Create a class named People.cs
a. Create a class named People.cs
*Create DBContext
a. Created a folder named “Infrastructure”, and then created a class named “MyDbContext
b. This class should inherit DbContext (We can think this coordinates Entity Framework functionality)
c. The DbSet is Table.
d. The “DefaultContext” is the name we specified.
a. Created a folder named “Infrastructure”, and then created a class named “MyDbContext
b. This class should inherit DbContext (We can think this coordinates Entity Framework functionality)
c. The DbSet is Table.
d. The “DefaultContext” is the name we specified.
*Create View
@Html.ActionLink("Put", "PutData",
"Home", null, null)
@Html.ActionLink("Get", "ReadData",
"Home", null, null)
|
@foreach (var ppl in ViewBag.ppls)
{
@Html.Raw("" +
ppl.Name + “<br>”);
}
|
*Create Controller
public ActionResult PutData() {
var db = new MyDbContext();
var ppl = new
People();
ppl.Name =
"TEST" + DateTime.Now.ToString();
db.Peoples.Add(ppl);
db.SaveChanges();
return
RedirectToAction("Succcess", "Home");
}
public ActionResult Succcess()
{
ViewBag.Message
= "Your data is put successfully.";
return View();
}
public ActionResult ReadData() {
var db = new
MyDbContext();
var ppls =
db.Peoples.ToList();
ViewBag.ppls =
ppls;
return View();
}
|
a. If we click the first link “Put”, the test data will be inserted to our database.
b. If we click the second link “Get”, as the
picture, the records will be loaded from database.
*Result
-------------------------------------------------
Use MS SQL Server to Check
*Type the server name from Azure Portal
*Extend it and view the design
Publish to Azure
1 *Right click the project -> Publish
*Click Create new with the option “Microsoft
Azure App Service”
*Select the resource group we created before.
*Created new App Service Plan
-------------------------------------------------
Migration
How about Change the model?
*Added new property for model
public class People
{
public int Id { get; set; }
public string Name { get; set;
}
public DateTime DateTime { get; set; }
}
|
*Added the controller
public ActionResult PutData() {
var db = new MyDbContext();
var ppl = new
People();
ppl.Name = "TEST" +
DateTime.Now.ToString();
ppl.DateTime = DateTime.Now;
db.Peoples.Add(ppl);
db.SaveChanges();
return
RedirectToAction("Succcess", "Home");
}
|
*Modify View
@{
ViewBag.Title =
"Result";
}
@foreach (var ppl in ViewBag.ppls)
{
@Html.Raw("" +
ppl.Name + "-" + ppl.DateTime + "<br>");
}
|
*After executing it, you might find a pop-up
message
a. Because the database has been created based on the previous model already, if we modify the model, the mapping between database and model will be failed.
a. Because the database has been created based on the previous model already, if we modify the model, the mapping between database and model will be failed.
*In order to conquer this issue, we need to enable
the migration feature (Ref: https://msdn.microsoft.com/en-us/data/jj591621)
*In VS, Tools -> NuGet Package Manager ->
Package Manager Console
Enable-Migrations
|
Enable-Migrations -EnableAutomaticMigrations
|
*We can run the following command on Console to
update
Update-Database
|
*After that, publish it again. See the result
below.