在vs2015上使用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.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”