当前位置: 移动技术网 > IT编程>开发语言>.net > .Net core,EFCore 入门

.Net core,EFCore 入门

2018年12月12日  | 移动技术网IT编程  | 我要评论

铁岭房屋出租,李一姐,金鹰英雄传

我在百度上搜了一下.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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网