.Net core,EFCore 入门

我在百度上搜了一下.net  core和efcore 入门案例。好多博客都是大概说了一下做法,对于小白而言还是一头雾水,我今天就抽出一点时间,写一个详细的入门小案例,就一张表没有什么业务可言。主要是操作的步骤,当然这只是让小白入个门,以后到公司工作,每个项目经理搭的架构不完全一样,但是我们懂了基本的,再做项目架构稍微复杂的就能很快上手,因为底层原理大同小异。话不多说我们开始动手做吧。

  1. 为了我们后期更好打开项目我们新建一个项目解决方案这个你们随意,咱们这个项目做

NETCOREDemo.

2.在解决方案下,打开VS2017新建项目,选择ASP.NET  Core  Web应用程序

3. ASP.NET Core 的版本自己可以选择,咱们这里选择2.0。选择空然后确定。

4.添加相关引用

有两种方式

第一种采用命令行:这个我就不多说了,可以百度一下命令行安装EFCore相关包(不同的数据库包也不一样,搜索的时候关键带上自己的数据库)

第二种简单好用:我就以SQLSERVER数据库为例,我们新建好的项目有个依赖项,我们右键>点击NuGet程序包

5.点浏览搜索一.Microsoft.EntityFrameworkCore.SqlSerVer 二. Microsoft.EntityFrameworkCore.Tools  这两个包然后安装

6.添加好引用后,继续设计数据库,采用EFCore  CodeFirst,我们先建立一个文件夹Models

在文件夹下添加这个类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace MyNoteItem.Models
{
    public class Note
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] // 主键自增id
        public int Id { get; set; }
        [Required]
        [MaxLength(100)]
        public string Title { get; set; }
        [Required]
        public string Content { get; set; }
        public DateTime Create { get;set; }
    }
}

  

7.接着在Models下创建一个NoteContext继承我们的上下文DbContext

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyNoteItem.Models
{
    public class NoteContext:DbContext
    {

        public NoteContext(DbContextOptions<NoteContext> options) : base(options)
        {

        }
        public DbSet<Note> Notes { get; set; }

    }
}

  

8.打开Startup.cs添加如下代码,当然连接串因自己的数据库用户和密码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using MyNoteItem.Models;
using MyNoteItem.Repository;

namespace MyNoteItem
{
    public class Startup
    {
      
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            var connection = @"Server=LAPTOP-OEENOHEO\LOCAL;DataBase=Note;UID=sa;PWD=sa123;";
            services.AddDbContext<NoteContext>(options=>options.UseSqlServer(connection));
            services.AddScoped<INoteRepository, NoteRepository>();

        }
        
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
          

            app.UseStaticFiles();

            app.UseMvc(routes =>   //为程序注册路由,默认打开的页面
        {
            routes.MapRoute(
                     name: "default",
                     template: "{controller=Note}/{action=Index}/{id?}");
        });
        }


    }
}

  

9.单击VS的菜单>工具>NuGet包管理器>程序包管理控制台,打开后在程序包管理器控制台执行如下命令:

Add-Migration  NoteFirst

Update-Database

执行完出现Done表示 成功,查看数据库,看是否生成对应的数据库。如出现错误检查一下数据库连接串是否正确。EF Core 默认生成的表名为复数形式,可以在NoteContext的OnModelCreating方法改写(具体可以百度)。

10.接下来项目一步步搭建,项目结构如下:

11.项目运行结果如下:

12.虽然是个入门demo,代码量还是有的,所以我放在了我的GitHub上,供大家免费下载,地址如下:

https://github.com/LZYSW/.NetCoreDemo1.git

后续分页等功能。让我期待下一期的到来吧!希望对大家有用。

posted on 2018-12-11 21:57  开发中123  阅读(884)  评论(2编辑  收藏  举报

导航