In the previous blog we wrote that the database connection of this software we use the EF Framework, code First mode, through the model class, when the controller is created directly to build the database, complete the database connection, and operation.
Before we use the EF framework, we need to write the model classes well. Then create the controller.
This software currently requires two model classes, in the previous blog, we have designed, and completed the model class, this time we just need to get the code to use it. Here are three classes of files: Team.cs, Ju.cs,score.cs, respectively, is the team class, the fractional class and the second class. The specific code is as follows:
public class Team
{
[Key]
public int TId {get; set;}
[Display (name = "team Name")]
public string Tname {get; set;}
[Display (Name = "result")]
public string TResult {get; set;}
public int Tparentid {get; set;}
}
public class Ju
{
[Key]
public int JId {get; set;}
[Display (Name = "innings")]
public string Juci {get; set;}
[Display (Name = "score")]
public string Jscore {get; set;}
public int Tjid {get; set;}
}
public class Score
{
[Key]
public int SId {get; set;}
[Display (Name = "score")]
public int Ascore {get; set;}
[Display (Name = "score")]
public int Bscore {get; set;}
[Display (Name = "Remarks")]
public string Note {get; set;}
public int Sjid {get; set;}
}
public class Countscoredbcontext:dbcontext
{
Public dbset<team> Team {get; set;}
Public dbset<ju> Ju {get; set;}
Public dbset<score> score {get; set;}
}
When two model classes are completed. We need to make a build of the software, right-click the project, and then choose Build, and not generate subsequent actions may be error.
After the model class is completed, we can then create a controller to connect to the database. Right-click on the Controller folder, click Add, add Controllers, the controller name is rewritten as: Teamcontroller, the final generation is:
public class Teamcontroller:controller
{
Private Countscoredbcontext db = new Countscoredbcontext ();
//
GET:/team/
Public ActionResult Index ()
{
Return View (db. Team.tolist ());
}
//
GET:/TEAM/DETAILS/5
Public actionresult Details (int id = 0)
{
Team Team = db. Team.find (ID);
if (team = = null)
{
return Httpnotfound ();
}
return View (team);
}
//
GET:/team/create
Public ActionResult Create ()
{
return View ();
}
//
POST:/team/create
[HttpPost]
[Validateantiforgerytoken]
Public ActionResult Create (Team team)
{
if (modelstate.isvalid)
{
db. Team.add (team);
db. SaveChanges ();
return redirecttoaction ("Create", "score");
}
Return View (Team);
}
//
GET:/TEAM/EDIT/5
Public actionresult Edit (int id = 0)
{
Team Team = db. Team.find (ID);
if (team = = null)
{
return Httpnotfound ();
}
return View (team);
}
//
POST:/TEAM/EDIT/5
[HttpPost]
[Validateantiforgerytoken]
Public ActionResult Edit (Team team)
{
if (modelstate.isvalid)
{
db. Entry (Team). state = entitystate.modified;
db. SaveChanges ();
return redirecttoaction ("Index");
}
return View (team);
}
//
GET:/TEAM/DELETE/5
Public actionresult Delete (int id = 0)
{
Team Team = db. Team.find (ID);
if (team = = null)
{
return Httpnotfound ();
}
return View (team);
}
//
POST:/TEAM/DELETE/5
[HttpPost, ActionName ("Delete")]
[Validateantiforgerytoken]
Public actionresult deleteconfirmed (int id)
{
Team Team = db. Team.find (ID);
db. Team.remove (team);
db. SaveChanges ();
return redirecttoaction ("Index");
}
protected override void Dispose (bool disposing)
{
Db. Dispose ();
Base. Dispose (disposing);
}
}
The attempt to correspond to the action method has also been established.
We can clearly see the files generated by the system for us. Then we can do the work on these files;
The same, Scorecontroller,jucontroller Controller controller is also established, because the audience interface does not involve the deletion of data to change, only one query, so not in this way, manually generated action methods and views.
Spectatorcontroller Specific generation:
Here we use the EF framework, complete the model class, then create the controller, connect the database, and access it. In the case of the EF framework, the database connection is very convenient and easy to use. At some point, however, the framework is cumbersome to use.
Completed the database connection, the implementation of the software is nearing completion, in the next blog, will be the final completion of the software, and the use of software functions and testing.
The MVC Volleyball Scoring Program--(vi) using frames, creating controllers, generating databases