標籤:
asp.net core+ef core
官方的文檔https://docs.asp.net/en/latest/tutorials/first-mvc-app/start-mvc.html
先來看一下實現的效果
開始之前,確定本機已經有.NET Core環境。https://www.microsoft.com/net/core#windows
1.建立解決方案的檔案結構如(模糊處理的過檔案是自己後面加的和ef產生的)。
2.要使用ef core,先引用ef core相關的程式包。https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html
開啟project.json,將“Microsoft.EntityFrameworkCore.SqlServer”、“Microsoft.EntityFrameworkCore.SqlServer.Design”和“Microsoft.EntityFrameworkCore.Design”添加到“dependencies”下;將“Microsoft.EntityFrameworkCore.Tools”添加到“tools”下。(註:當然這些都可以通過NuGet來安裝,需要注意的是用nuget安裝“Microsoft.EntityFrameworkCore.Tools”時,要將其移動到“tools”下)
3.開啟串連到資料庫,設定資料庫連接字串
4.開啟程式包管理主控台,運行下面命令。-OutputDir Models指向組建檔案存放檔案的位置是Models
Scaffold-DbContext "Server=.;Database=CoreDemo;Trusted_Connection=True;User ID=你的串連資料庫的使用者;Password=你的串連資料庫的密碼;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
5.開啟剛才用Scaffold-DbContext產生的CoreDemoContext.cs的檔案,移除OnConfiguring方法
6.在CoreDemoContext.cs裡面加入下面代碼
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.TUsers14 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.Name35 };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
asp.net core+ef core