ORM(Object Relational Mapping):对象关系映射;
笔记中演示的开发环境:
VS2019 Core 3.1;
NHibernate5.3.12;
MySql.Data8.0.29;
NHibernate.Mapping.Attributes;
MariaDB 5.5.68;
测试使用postman;
数据库可视化工具使用Navicat Premium
一、创建项目
笔记中使用一个Core框架的Web API项目为例,部分内容跟Nhibernate配置没有关系,不感兴趣可自行创建其他类型项目;
1)、创建项目
F5运行项目,
看到浏览器中的Json字符串表示项目创建成功;
2)、删除不必要的系统自建Demo文件,
WeatherForecastController.cs;
WeatherForecast.cs;
3)、添加控制器
4)、修改API调试状态下的配置文件lanchSettings.json
这个文件中的配置跟发布后的文件没有关系,只是VS调试的时候会起作用;
原来的文件内容如下:
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:62978",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"NhibernateDemol": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
修改后如下:
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"NhibernateDemol": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "NHibernateTest/gt",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
webapi的调试配置文件这么改的原因在之前关于webapi的笔记中已经提过,不在重复;
5)、修改控制器代码
文件:NHibernateTestController.cs
using Microsoft.AspNetCore.Mvc;
namespace NhibernateDemo.Controllers
{
[Route("[controller]")]
[ApiController]
public class NHibernateTestController : ControllerBase
{
[Route("gt")]
[HttpGet]
public ContentResult a()
{
return Content("This is a WebApiTest with GetMethod.");
}
}
}
添加一个测试webapi的get方法,测试项目创建成功;