在vs2015上使用asp.net core+ef core

官方的文檔https://docs.asp.net/en/latest/tutorials/first-mvc-app/start-mvc.htmlhtml

先來看一下實現的效果jquery

 開始以前,肯定本機已經有.NET Core環境。https://www.microsoft.com/net/core#windowssql

1.建立解決方案的文件結構以下圖(模糊處理的過文件是本身後面加的和ef生成的)。數據庫

2.要使用ef core,先引用ef core相關的程序包。https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.htmljson

打開project.json,將「Microsoft.EntityFrameworkCore.SqlServer」、「Microsoft.EntityFrameworkCore.SqlServer.Design」和「Microsoft.EntityFrameworkCore.Design」添加到「dependencies」下;將「Microsoft.EntityFrameworkCore.Tools」添加到「tools」下。(注:固然這些均可以經過NuGet來安裝,須要注意的是用nuget安裝「Microsoft.EntityFrameworkCore.Tools」時,要將其移動到「tools」下)windows

3.打開鏈接到數據庫,設置數據庫鏈接字符串mvc

4.打開程序包管理控制檯,運行下面命令。-OutputDir Models指向生成文件存放文件的位置是Modelsapp

Scaffold-DbContext "Server=.;Database=CoreDemo;Trusted_Connection=True;User ID=你的鏈接數據庫的用戶;Password=你的鏈接數據庫的密碼;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

 

5.打開剛纔用Scaffold-DbContext生成的CoreDemoContext.cs的文件,移除OnConfiguring方法asp.net

6.在CoreDemoContext.cs裏面加入下面代碼async

1 public CoreDemoContext(DbContextOptions<CoreDemoContext> option)
2       : base(option)
3 { }

7.打開appsettings.json,加入下面的數據鏈接配置代碼

"ConnectionStrings": {
    "CoreDemoDatabase": "Server=.;Database=CoreDemo;Trusted_Connection=True;User ID=你的鏈接數據庫的用戶;Password=你的鏈接數據庫的密碼;"
  }

8.打開Startup.cs,修改ConfigureServices方法,以下

1 public void ConfigureServices(IServiceCollection services)
2 {
3       // Add framework services.
4       services.AddMvc();
5       services.AddDbContext<CoreDemoContext>(option => option.UseSqlServer(Configuration.GetConnectionString("CoreDemoDatabase"))); // 獲取sql鏈接配置
6 }

9.添加一個新的Controller--UserController

 1     public class UserController : Controller
 2     {
 3         private CoreDemoContext _context;
 4 
 5         public UserController(CoreDemoContext context)
 6         {
 7             _context = context;
 8         }
 9 
10         // GET: /<controller>/
11         public IActionResult Index()
12         {
13             var users = (from u in _context.TUsers
14                          select u).ToList();
15             return View(users);
16         }
17 
18         public IActionResult Create()
19         {
20             return View();
21         }
22 
23         [HttpPost]
24         [ValidateAntiForgeryToken]
25         public async Task<IActionResult> Create(UserViewModel model)
26         {
27             if (!ModelState.IsValid)
28             {
29                 return View(model);
30             }
31 
32             TUsers user = new TUsers()
33             {
34                 Name = model.Name
35             };
36             _context.TUsers.Add(user);
37             int result = await _context.SaveChangesAsync();
38             return RedirectToAction("Index");
39         }
40     }

10.建立Index視圖和Create視圖

 1 @model IEnumerable<TUsers>
 2 
 3 @{
 4     ViewBag.Title = "用戶列表";
 5 }
 6 
 7 <h2>用戶列表</h2>
 8 <p>
 9     <a asp-action="Create">添加用戶</a>
10 </p>
11 
12 <table class="table table-bordered table-hover table-striped">
13     <tr>
14         <th>姓名</th>
15         <th>建立日期</th>
16     </tr>
17     @foreach (var item in Model)
18     {
19         <tr>
20             <td>
21                 @Html.DisplayFor(modelItem => item.Name)
22             </td>
23             <td>
24                 @(((DateTime)item.CreateDate).ToString("yyyy-MM-dd hh:mm:ss"))
25             </td>
26         </tr>
27     }
28 </table>
 1 @model WebDemo.Models.UserViewModel
 2 
 3 @{
 4     ViewBag.Title = "新增用戶";
 5 }
 6 
 7 <h2>建立一個新用戶</h2>
 8 
 9 <form asp-action="Create" method="post">
10     <div class="form-horizontal">
11         @*<div asp-validation-summary="All" class="text-danger"></div>*@
12         <div class="form-group">
13             <label asp-for="Name" class="col-md-2 control-label">姓名:</label>
14             <div class="col-md-10">
15                 <input asp-for="Name" class="form-control" />
16                 <span asp-validation-for="Name" class="text-danger"></span>
17             </div>
18         </div>
19         <div class="form-group">
20             <div class="col-md-offset-2 col-md-10">
21                 <input type="submit" value="Create" class="btn btn-default" />
22             </div>
23         </div>
24     </div>
25 </form>
26 
27 @section scripts{
28     @* 添加jquery驗證庫 *@
29     <environment names="Development">
30         <script src="~/lib/jquery-validation/dist/jquery.validate.js" asp-append-version="true"></script>
31         <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js" asp-append-version="true"></script>
32     </environment>
33     <environment names="Staging,Production">
34         <script src="~/lib/jquery-validation/dist/jquery.validate.min.js" asp-append-version="true"></script>
35         <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js" asp-append-version="true"></script>
36     </environment>
37 }

11.到這一步,運行項目能夠直接用vs啓動,也能夠在cmd裏面使用「dotnet run

 

轉載請標註原文地址:http://www.cnblogs.com/JasonLong/p/5653273.html

相關文章
相關標籤/搜索