ASP.NET Core 開發 - Entity Framework (EF) Core

 

 

EF Core 1.0 Database First http://www.cnblogs.com/linezero/p/EFCoreDBFirst.html

ASP.NET Core 開發 - Entity Framework (EF) Core,ASP.NET Core 操做數據庫。javascript

Entity Framework (EF) Core RC2 也發佈了,能夠適用於 .NET Core 及ASP.NET Core 。html

EntityFrameworkCore SQLite 本篇文章以SQLite 數據庫做爲介紹。java

目前 EF Core 支持的數據庫:git

  • Microsoft SQL Server
  • SQLite
  • Postgres (Npgsql)
  • SQL Server Compact Edition
  • InMemory (for testing purposes)

後面將會增長:github

  • MySQL
  • IBM DB2

 

介紹完了,如今正式開始。web

新建項目

這裏咱們選擇  ASP.NET Core Web Application (.NET Core) sql

這裏選擇web 應用程序,而後更改身份驗證 改成 不進行身份驗證數據庫

 

引用Entity Framework (EF) Core

NuGet官方源已經支持 .NET Core RC2 的相關引用。json

而後在 NuGet命令行下安裝 ,咱們也能夠使用NuGet包管理器安裝。ide

Install-Package Microsoft.EntityFrameworkCore.Sqlite –Pre

建立實體

咱們在項目添加一個 Models 文件夾。

新建一個User.cs

    public class User { public int Id { get; set; } public string UserName { get; set; } public string Password { get; set; } }

這裏我爲了方便,繼續新建 DataContext.cs

複製代碼
    public class DataContext : DbContext { public DataContext(DbContextOptions<DataContext> options) : base(options) { } public DbSet<User> Users { get; set; } }
複製代碼

建立數據庫

打開 Startup.cs  在 ConfigureServices 下添加以下代碼:

複製代碼
        public void ConfigureServices(IServiceCollection services) { var connection = "Filename=./efcoredemo.db"; services.AddDbContext<DataContext>(options => options.UseSqlite(connection)); // Add framework services.  services.AddMvc(); }
複製代碼

添加好之後,咱們來安裝 Microsoft.EntityFrameworkCore.Tools 

Install-Package Microsoft.EntityFrameworkCore.Tools –Pre

安裝好之後,咱們在 project.json tools 節點下

複製代碼
"tools": { "Microsoft.EntityFrameworkCore.Tools": { "version": "1.0.0-preview1-final", "imports": [ "portable-net45+win8+dnxcore50", "portable-net45+win8" ] },
複製代碼

 

開始建立數據庫 使用 dotnet ef

打開文件夾的命令行,

輸入

  dotnet ef migrations add MyFirstMigration

  dotnet ef database update

這樣咱們就建立好了數據庫。更多命令請 dotnet ef -h

項目使用

新建一個 UserController

而後 在Views 添加一個 User 文件,而後添加對應的視圖。

添加一個Register Action,再添加一個 Register 視圖

複製代碼
@model EFCoreDemo.Models.User
@{ 
    ViewBag.Title = "用戶添加";
}
<form asp-controller="User" asp-action="Register" method="post"> <div class="form-group"> <label asp-for="UserName" class="col-md-2 control-label">用戶名:</label> <div class="col-md-10"> <input class="form-control" asp-for="UserName" /> <span asp-validation-for="UserName" class="text-danger"></span> </div> <label asp-for="Password" class="col-md-2 control-label">密碼:</label> <div class="col-md-10"> <input class="form-control" asp-for="Password" /> <span asp-validation-for="Password" class="text-danger"></span> </div> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="保存" class="btn btn-default" /> </div> </div> </form>
複製代碼

UserController.cs

複製代碼
    public class UserController : Controller { private DataContext _context; public UserController(DataContext context) { _context = context; } // GET: /<controller>/ public IActionResult Index() { return View(_context.Users.ToList()); } public IActionResult Register() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public IActionResult Register(User registeruser) { if (ModelState.IsValid) { _context.Users.Add(registeruser); _context.SaveChanges(); return RedirectToAction("Index"); } return View(registeruser); } }
複製代碼

 

程序運行起來:

http://localhost:5000/User/Register

列表展現 

Index.cshtml

複製代碼
@model IEnumerable<EFCoreDemo.Models.User>

@{
    ViewBag.Title = "用戶"; } <table class="table"> <tr> <th>Id</th> <th>用戶名</th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Id) </td> <td> @Html.DisplayFor(modelItem => item.UserName) </td> </tr> } </table>
複製代碼

 

http://localhost:5000/User

參考文檔:https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html

若是你以爲本文對你有幫助,請點擊「推薦」,謝謝。

.NET Core 跨平臺交流羣: 550897034 .NET Core跨平臺 博客示例代碼: GitHub
相關文章
相關標籤/搜索