EF Core CodeFirst实践 ( 使用MS SqlServer)

这里使用 MS SQLSERVER ,网上大多使用 SQLite

先来一个CodeFirst

新建项目

这里我们选择  ASP.NET Core Web Application (.NET Core) 

这里选择web 应用程序,然后更改身份验证 改为 不进行身份验证

然后再包管理控制台里执行下面两条命令

 

引用 EntityFrameworkCore

Install-Package Microsoft.EntityFrameworkCore

再引用 EntityFrameworkCore.SqlServer

Install-Package Microsoft.EntityFrameworkCore.SqlServer

创建实体

我们在项目添加一个 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 = "Data Source=.;Initial Catalog=EFCore;User ID=sa;Password=sa.123"; services.AddDbContext<DataContext>(options => options.UseSqlite(connection)); // Add framework services. services.AddMvc(); }
复制代码

添加好以后,我们来安装 Microsoft.EntityFrameworkCore.Tools 

Install-Package Microsoft.EntityFrameworkCore.Tools –Pre

在文件资源管理器中找到这个项目,找到 project.json文件

 

 

在 tools节点下 增加代码

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

效果如下图

 

之后  开始创建数据库 使用 dotnet ef 命令

先打开cmd 窗口 ,跳转到当前项目文件夹

输入

  dotnet ef migrations add MyFirstMigration

 

再输入

  dotnet ef database update

这样数据库就创建好了

注意
如果 IIS-Express 在运行中,你会遇到错误
CS2012: Cannot open 'MvcMovie/bin/Debug/netcoreapp1.0/MvcMovie.dll' for writing -- 'The process cannot access the file
'MvcMovie/bin/Debug/netcoreapp1.0/MvcMovie.dll'
because it is being used by another process.'

dotnet ef 命令

  • dotnet (.NET Core) 是 .NET 的跨平台实现。你可以在这里了解它。
  • dotnet ef migrations add Initial 运行 Entity Framework .NET Core CLI 迁移命令并创建初始化迁移。参数 "Initial" 可以是任意值,但是通常用这个作为第一个(初始的) 数据库迁移。这个操作创建了一个 *Data/Migrations/_Initial.cs* 文件,这个文件包含了添加(或删除)Movie 表到数据库的迁移命令。
  • dotnet ef database update dotnet ef database update 用我们刚刚创建的迁移来更新数据库。

 

 添加  UserController
public class UserController : Controller
    {
        private efcoredemoContext _context;

        public UserController(efcoredemoContext context)
        {
            _context = context;
        }

        // GET: /<controller>/
        public IActionResult Index()
        {
            return View(_context.Users.ToList());
        }
    }

添加 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>

然后就可以运行啦

 

感谢  ASP.NET Core 开发 - Entity Framework (EF) Core

posted @ 2016-09-14 10:23  宝宝董  阅读(14071)  评论(3编辑  收藏  举报