zoukankan      html  css  js  c++  java
  • [NetCore学习记录]第一章.使用netcore撸个简单的增删改查

    1.引言

    2.解决方案各部分介绍图

    3.添加数据模型

    4.添加数据库上下文

    5.修改配置文件

    6.使用依赖关系注入容器注册数据库上下文

    7.添加基架工具并执行初始迁移

    1.引言

    NetCore出来有一段时间了,跨平台、开源、高性能 让每个从事.net的开发者都兴奋了一把,对此我也有浓厚的兴趣。

    2.解决方案各部分介绍图

    3.添加数据模型

    namespace StudyRazorWeb.Models
    {
        public class User
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public bool Sex { get; set; }
            public string Adress { get; set; }
            public string Tel { get; set; }
        }
    }

    4.添加数据库上下文

    数据库上下文是为给定数据库模型协调实体框架功能的主类

    namespace StudyRazorWeb.Models
    {
        public class UserContext:DbContext
        {
            public UserContext(DbContextOptions<UserContext> options)
                :base(options)
            {
    
            }
            public DbSet<User> User { get; set; }
        }
    }

    5.修改配置文件(appsettings.json)

    {
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "ConnectionStrings": {
        "UserContext": "server=服务器地址;Database=数据库名称;uid=用户名;pwd=密码"
      }
    }

    6.使用依赖关系注入容器注册数据库上下文,修改Startup.cs文件

     public void ConfigureServices(IServiceCollection services)
    { services.AddDbContext
    <UserContext>(options => options.UseSqlServer(Configuration.GetConnectionString("UserContext"))); services.AddMvc(); }

    7.添加基架工具并执行初始迁移

    • 添加 Visual Studio Web 代码生成包。 必须添加此包才能运行基架引擎。
    • 添加初始迁移
    • 使用初始迁移更新数据库

    从“工具”菜单中,选择“NuGet包管理器”>"包管理器控制台"

    在PMC中输入一下命令:

    //安装运行基架引擎所需的工具
    Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 2.0.0
    //生成用于创建初始数据库架构的代码,此架构以DbContext中指定的模型为基础 Add-Migration Initial
    //用于创建数据库的Migrations/_Initial.cs文件中运行Up方法 Update
    -Database

    ”搭建用户模型的基架”

    使用命令窗口切换,打开项目目录(包含 Program.cs、Startup.cs 和 .csproj 文件的目录)

    运行下面的命令

    dotnet aspnet-codegenerator razorpage -m User -dc UserContext -udl -outDir PagesUsers --referenceScriptLibraries

    8.写在后面

    文章非常基础,简单,没有源码解析,也没有各大语言性能对比,是个入门篇,后续会出一个系列。。。

    天道酬勤,大道至简,坚持

  • 相关阅读:
    汇编语言 第二单元 整理
    iOS10推送必看UNNotificationServiceExtension
    RSA加,解密
    添加购物车动画
    长按移动cell
    http live streming
    修改工程
    searbar
    tableView 编辑模式
    iOS 3D touch
  • 原文地址:https://www.cnblogs.com/jdzhang/p/8035448.html
Copyright © 2011-2022 走看看