C# Core WebAPI+Nhibernate的ORM配置,及使用NHibernate.Mapping.Attributes实现代码代替映射配置文件

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方法,测试项目创建成功; 

6)、​​运行代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值