netcore 使用NHibernate

一、安装

  <ItemGroup>
    <PackageReference Include="FluentNHibernate" Version="3.1.0" />
    <PackageReference Include="NHibernate.Driver.MySqlConnector" Version="2.0.3" />
  </ItemGroup>

二、新建实体及映射

Store.cs

namespace FluentNHibernateDemo.Entities
{
    public class Store
    {
        public virtual int Id { get; protected set; }
        public virtual string Name { get; set; }
    }
}

StoreMap.cs

using FluentNHibernate.Mapping;
using FluentNHibernateDemo.Entities;

namespace Examples.FirstProject.Mappings
{
    public class StoreMap : ClassMap<Store>
    {
        public StoreMap()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            Table("stores");
        }
    }
}

三、操作数据WeatherForecastController.cs

using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Cfg;
using FluentNHibernateDemo.Entities;
using Microsoft.AspNetCore.Mvc;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Cfg;

namespace FluentNHibernateDemo.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IActionResult Get()
        {
            var sessionFactory = CreateSessionFactory();

            using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var store = new Store { Name = "Bargin Basin"+DateTime.Now };
                    session.Save(store);
                    transaction.Commit();
                }
            }

            return Ok(true);
        }

        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure(new NHibernate.Cfg.Configuration().DataBaseIntegration(c => c.MySqlConnectorDriver()))
                .Database(MySQLConfiguration.Standard.ConnectionString("Server=192.168.31.132;User ID=root;Password=123456;Database=sugar;port=3306"))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();
        }
        private static void BuildSchema(NHibernate.Cfg.Configuration config)
        {
            new SchemaExport(config)
                .Create(script =>
                {
                    Console.WriteLine(script);
                }, false);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值