创建NetCore2.2 Web项目+EFCore+SQLServer
在空余时间学习下NetCore,记录日常,供参考。
1.确保已下载安装NetCore2.2SDK 环境,下载地址:https://dotnet.microsoft.com/download/dotnet-core/2.2

2.打开VS2017,首先新建一个解决方案,并在解决方案上新建项目操作,选择ASP.NET Core Web 应用程序,点击“确定”。继续,NetCore版本选择ASP.NET Core 2.2,类型选择“Web应用程序”点击确定。


3、在appsettings.json添加配置数据库链接字符串,添加后如下图
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": { "DefaultConnectionString": "Data Source=.;Initial Catalog=bcmf_core;User ID=sa;Password=123456" }
}
4.NetCore2.2的SDK正常包含有Microsoft.EntityFrameworkCore和microsoft.EntityFrameworkCore.SqlServer,若没有,可以在NuGet包中查询安装,这里也是安装的2.2版本
NuGet官网:https://www.nuget.org/
搜索以上两个引用名称,获取安装命令行,在VS2017的程序包管理器控制台输入对应命令行,如下:

5.在Models文件夹新建文件夹“Entity”,在Entity下存放我们的数据实体类,新建实体类User.cs,这里视个人情况而定,我选择的是以前项目的一个测试数据库。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace NetCoreCMS.Models.Entity
{
public class User
{
public int UserId { get; set; }
public int PermissionId { get; set; }
public int RoleId { get; set; }
public string Name { get; set; }
public string NameCN { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public bool IsSystem { get; set; }
public bool IsActive { get; set; }
}
/// <summary>
/// 返回模型类
/// </summary>
public class UserViewModel
{
public int UserId { get; set; }
public int PermissionId { get; set; }
public int RoleId { get; set; }
public string Name { get; set; }
public string NameCN { get; set; }
public string Email { get; set; }
public string IsSystem { get; set; }
public string IsActive { get; set; }
}
}
5、在Models文件夹新建文件夹“Mapping”,在Mapping下存放我们的数据实体映射类,新建实体类UserMapping.cs,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CMSCore.Web.Models.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; namespace CMSCore.Web.Models.Mapping
{
public class UserMapping : IEntityTypeConfiguration<User>
{
void IEntityTypeConfiguration<User>.Configure(EntityTypeBuilder<User> builder)
{
builder.ToTable("User");//对应数据库User表
builder.HasKey("UserId");
builder.Property(t => t.PermissionId).HasColumnName("PermissionId").IsRequired();
builder.Property(t => t.RoleId).HasColumnName("RoleId").IsRequired();
builder.Property(t => t.Name).HasColumnName("Name").IsRequired().HasMaxLength();
builder.Property(t => t.NameCN).HasColumnName("NameCN").IsRequired().HasMaxLength();
builder.Property(t => t.Email).HasColumnName("Email").IsRequired().HasMaxLength();
builder.Property(t => t.Password).HasColumnName("Password").IsRequired();
builder.Property(t => t.IsSystem).HasColumnName("IsSystem").IsRequired();
builder.Property(t => t.IsActive).HasColumnName("IsActive").IsRequired();
}
}
}
6、在Models文件夹新建上下文BcmfContext.cs类
using CMSCore.Web.Models.Entity;
using CMSCore.Web.Models.Mapping;
using Microsoft.EntityFrameworkCore; namespace CMSCore.Web.Models
{
public class BcmfDBContext : DbContext
{
public BcmfDBContext(DbContextOptions options) : base(options)
{
}
public DbSet<User> User { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new UserMapping());
base.OnModelCreating(modelBuilder);
}
}
}
7、在入口文件注册上下文,在根目录Startup.cs的ConfigureServices方法中,注册上下文。
不要忘记了添加引用命名空间
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
}); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
//注册数据库操作上下文
services.AddDbContext<BcmfDBContext>(option => option.UseSqlServer(Configuration.GetConnectionString("DefaultConnectionString")));
}
8、在Controller文件夹下,创建控制器UserController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CMSCore.Web.Models;
using CMSCore.Web.Models.Entity;
using Microsoft.AspNetCore.Mvc; // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace CMSCore.Web.Controllers
{
public class UserController : Controller
{
private readonly BcmfDBContext db; public UserController(BcmfDBContext _db)
{
db = _db;
} // GET: /<controller>/
public IActionResult Index()
{
var userList = db.User.Where(m => m.IsActive).Select(t => new UserViewModel
{
Email = t.Email,
IsActive = t.IsActive ? "激活" : "禁用",
IsSystem = t.IsSystem ? "是" : "否",
Name = t.Name,
NameCN = t.NameCN,
PermissionId = t.PermissionId,
RoleId = t.RoleId,
UserId = t.UserId
});
return Json(userList);
}
}
}
运行、访问/user/index,成功获取用户信息
创建NetCore2.2 Web项目+EFCore+SQLServer的更多相关文章
- step2-------使用myeclipse创建maven java web项目
1.文章内容概述: 在对项目需求进行分析之后,决定使用maven对我的java web项目进行管理,这篇文章记录了使用myeclipse创建maven java web项目的过程. 2.开发环境: j ...
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(0):专题导航 [持续更新中]
写在前面的话 随着 Visual Studio 2013 的正式推出,ASP.NET 和 Visual Studio Web 开发工具 也发布了各自的最新版本. 新版本在构建 One ASP.NET ...
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(1):概述 - 创建 Web 应用程序项目
注:本文是“在 Visual Studio 2013 中创建 ASP.NET Web 项目”专题的一部分,详情参见 专题导航 . 预备知识 本专题适用于 Visual Studio 2013 及以上版 ...
- IDEA如何创建及配置Web项目(多图)
正文之前 在学习Java Web时,第一个遇到的问题就是如何创建或配置Web项目了,今天,就用IntelliJ IDEA 来进行Web项目配置: 创建Web项目 配置web项目 正文 创建Web项目 ...
- Java web 开发填坑记 2 -如何正确的创建一个Java Web 项目
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/72566261 本文出自[赵彦军的博客] Java web 开发填坑记 1-如何正确 ...
- Eclipse创建一个Maven Web项目
在这篇文章中,我们将演示如何在Eclipse IDE中使用maven创建一个动态Web项目. 使用的工具和技术 - Eclipse Jee Oxygen Maven 3.3.3 JavaSE 1.8 ...
- 创建一个动态Web项目:
开始你的Eclipse,然后进入“文件”>“新建”>“动态Web项目,然后输入项目名称为HelloWorldStruts2和设置其他的选项,在下面的屏幕: 选择在屏幕上的所有默认选项,最后 ...
- maven 学习---用Eclipse创建一个Maven Web项目
下面是使用 Eclipse 来创建一个Maven Web项目的说明.这是相当简单的. 现在让我们开始吧! 1: 启动 Eclipse, 点击 File->New->Other 2: 在弹出 ...
- 【Java Web】IDEA如何创建及配置Web项目(多图)
正文之前 在学习Java Web时,第一个遇到的问题就是如何创建或配置Web项目了,今天,就用IntelliJ IDEA 来进行Web项目配置: 创建Web项目 配置web项目 正文 创建Web项目 ...
随机推荐
- C#---数据库访问通用类、Access数据库操作类、mysql类 .
//C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System. ...
- 用SpringMVC实现的上传下载
1.导入相关jar包 commons-fileupload.jar commons-io.jar 2.配置web.xml文件 <?xml version="1.0" enco ...
- [转]MySQL存储过程
转自:http://www.cnblogs.com/exmyth/p/3303470.html 14.1.1 创建存储过程 MySQL中,创建存储过程的基本形式如下: CREATE PROCEDURE ...
- C#手机充值系统开发(基于聚合数据)
说是手机充值系统有点装了,其实就是调用了聚合数据的支付接口,其实挺简单的事 但是我发现博客园竟然没有类似文章,我就个出头鸟把我的代码贡献出来吧 首先说准备工作: 去聚合数据申请账号-添加手机支付的认证 ...
- 常用的几个Dos命令-持续更新中
1.服务相关 (1).查看服务 C:\Windows\system32>net start 已经启动以下 Windows 服务: (2).启动服务 C:\Windows\system32> ...
- Pro ASP.NET Core MVC 第6版 第二章(后半章)
增加动态输出 整个web应用平台的关注点在于构建并显示动态输出内容.在MVC里,控制器负责构建一些数据并将其传给视图.视图负责渲染成HTML. 从控制器向视图传递数据的一种方式是使用ViewBag 对 ...
- 解决 HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException:
HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException: The foll ...
- linux环境下为php7装phpredis扩展
phpredis在php7.php5下都有不同的版本,装岔了可能会编译报错,所以在安装之前请先看下自己的php是啥版本. 我的redis装的是redis3.2.3版本. 用phpinfo()查看安装的 ...
- std list/vector sort 自定义类的排序就是这么简单
所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数compare_index.但是要注意对象和对象指针的排序区别. 1.容器中是对象时,用操作符<或者比较函数,比较函数 ...
- Gym - 101550A(Artwork 倒序+并查集)
题目: 思路: 1.对输入数据离线,先把所有的黑线都画出来,统计一下剩余的白色连通块的个数,dfs过程将一个连通块放到一个集合中. 2.倒着往前消去黑线,如果当前的块A是白块就看他的四周有没有白块:有 ...