OsharpNS轻量级.net core快速开发框架简明入门教程

教程目录

  1. 从零开始启动Osharp

    1.1. 使用OsharpNS项目模板创建项目

    1.2. 配置数据库连接串并启动项目

    1.3. OsharpNS.Swagger使用实例(登录和授权)

    1.4. Angular6的前端项目启动

  2. Osharp代码生成器的使用

    2.1 生成器的使用

    2.2 生成代码详解(如何自己实现业务功能)

  3. Osharp部分模块使用

    3.1 Osharp.Redis使用

    3.2 Osharp.Hangfire使用

    3.3 Osharp.Permissions使用

  4. Osharp深度学习和使用

    4.1 切换数据库(从SqlServer改为MySql)

    4.2 多上下文配置(多个数据库的使用)

    4.3. 自定义模块的定义(Senparc.Weixin的使用)

    4.4. 继续学习中....

OsharpNS官方资源

项目地址:https://github.com/i66soft/osharp-ns20

演示地址:https://www.osharp.org 直接使用QQ登录可以查看效果

文档地址:https://docs.osharp.org 正在完善中....

发布博客:https://www.cnblogs.com/guomingfeng/p/osharpns-publish.html 大神看这个文档应该就能跑起来,从零开始启动Osharp基于此文档完成

VS生成器插件:https://marketplace.visualstudio.com/items?itemName=LiuliuSoft.osharp

官方交流QQ群:85895249

多上下文配置(多个数据库的使用)

  1. 项目CanDoo.Test.Core通过Nuget添加对包OsharpNS的引用

  2. 配置文件appsettings.Development.json中添加OSharp:DbContexts:MySqlAudit连接参数

"MySqlAudit": {
"DbContextTypeName": "CanDoo.Test.Core.Entity.MySqlAuditDbContext,OSharp.EntityFrameworkCore",//这里要注意下
"ConnectionString": "Server=localhost;Port=3306;UserId=root;Password=******;Database=CanDoo.Test.Audit;charset='utf8';Allow User Variables=True",
"DatabaseType": "MySql",
"LazyLoadingProxiesEnabled": true,
"AuditEntityEnabled": true,
"AutoMigrationEnabled": true
}
  1. 新建CanDoo.Test.Core.Entity.MySqlAuditDbContext上下文
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using OSharp.Entity; namespace CanDoo.Test.Core.Entity
{
public class MySqlAuditDbContext : DbContextBase
{
public MySqlAuditDbContext(DbContextOptions options, IEntityManager entityManager, IServiceProvider serviceProvider) : base(options, entityManager, serviceProvider)
{ }
}
}
  1. 创建CanDoo.Test.Web.Startups.MySqlAuditMigrationPack迁移模块
using System;
using OSharp.Entity;
using OSharp.Entity.MySql;
using CanDoo.Test.Core.Entity;
using CanDoo.Test.Web.Startups; namespace CanDoo.Test.Web.Startups
{
/// <summary>
/// MySqlAudit迁移模块
/// </summary>
public class MySqlAuditMigrationPack : MigrationPackBase<MySqlAuditDbContext>
{
/// <summary>
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动,
/// 级别默认为0,表示无依赖,需要在同级别有依赖顺序的时候,再重写为>0的顺序值
/// </summary>
public override int Order => 2; protected override DatabaseType DatabaseType { get; } = DatabaseType.MySql; protected override MySqlAuditDbContext CreateDbContext(IServiceProvider scopedProvider)
{
return new MySqlAuditDesignTimeDbContextFactory(scopedProvider).CreateDbContext(new string[0]);
} //针对多库连接的,需要在EntityConfiguration部分增加以下代码,指定DbContext
//public override Type DbContextType { get; } = typeof(MySqlAuditDbContext);
}
}
using System;
using System.Reflection; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using OSharp.Core.Options;
using OSharp.Data;
using OSharp.Entity;
using OSharp.Exceptions;
using OSharp.Extensions;
using OSharp.Reflection;
using CanDoo.Test.Core.Entity; namespace CanDoo.Test.Web.Startups
{
public class MySqlAuditDesignTimeDbContextFactory : DesignTimeDbContextFactoryBase<MySqlAuditDbContext>
{
private readonly IServiceProvider _serviceProvider; public MySqlAuditDesignTimeDbContextFactory()
{ } public MySqlAuditDesignTimeDbContextFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
} public override string GetConnectionString()
{
if (_serviceProvider == null)
{
IConfiguration configuration = Singleton<IConfiguration>.Instance;
string str = configuration["OSharp:DbContexts:MySqlAudit:ConnectionString"]; //这里是配置节点的信息 记得修改
return str;
}
OsharpOptions options = _serviceProvider.GetOSharpOptions();
OsharpDbContextOptions contextOptions = options.GetDbContextOptions(typeof(DefaultDbContext));
if (contextOptions == null)
{
throw new OsharpException($"上下文“{typeof(MySqlAuditDbContext)}”的配置信息不存在");
}
return contextOptions.ConnectionString;
} public override IEntityManager GetEntityManager()
{
if (_serviceProvider != null)
{
return _serviceProvider.GetService<IEntityManager>();
}
IEntityConfigurationTypeFinder typeFinder = new EntityConfigurationTypeFinder(new AppDomainAllAssemblyFinder());
IEntityManager entityManager = new EntityManager(typeFinder);
entityManager.Initialize();
return entityManager;
} public override bool LazyLoadingProxiesEnabled()
{
if (_serviceProvider == null)
{
IConfiguration configuration = Singleton<IConfiguration>.Instance;
return configuration["OSharp:DbContexts:MySqlAudit:LazyLoadingProxiesEnabled"].CastTo(false); //这里是配置节点的信息 记得修改
}
OsharpOptions options = _serviceProvider.GetOSharpOptions();
OsharpDbContextOptions contextOptions = options.GetDbContextOptions(typeof(DefaultDbContext));
if (contextOptions == null)
{
throw new OsharpException($"上下文“{typeof(MySqlAuditDbContext)}”的配置信息不存在");
} return contextOptions.LazyLoadingProxiesEnabled;
} public override DbContextOptionsBuilder UseSql(DbContextOptionsBuilder builder, string connString)
{
string entryAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
Console.WriteLine($"entryAssemblyName: {entryAssemblyName}");
return builder.UseMySql(connString, b => b.MigrationsAssembly(entryAssemblyName));
}
}
}
  1. 审计功能相关的表使用新的上下文,CanDoo.Test.EntityConfiguration.Systems中Audit开头的3个文件都增加以下配置代码,指定使用MySqlAuditDbContext
public override Type DbContextType { get; } = typeof(MySqlAuditDbContext); //新增此行代码 指定使用MySqlAuditDbContext 未指定的还是使用DefaultDbContext
using System;
using System.Collections.Generic;
using System.Text;
using CanDoo.Test.Core.Entity;
using CanDoo.Test.Systems.Entities;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using OSharp.Entity; namespace CanDoo.Test.EntityConfiguration.Systems
{
public class AuditPropertyConfiguration : EntityTypeConfigurationBase<AuditProperty, Guid>
{
public override Type DbContextType { get; } = typeof(MySqlAuditDbContext); //新增此行代码 指定使用MySqlAuditDbContext 未指定的还是使用DefaultDbContext /// <summary>
/// 重写以实现实体类型各个属性的数据库配置
/// </summary>
/// <param name="builder">实体类型创建器</param>
public override void Configure(EntityTypeBuilder<AuditProperty> builder)
{
builder.HasIndex(m => m.AuditEntityId);
builder.HasOne(m => m.AuditEntity).WithMany(n => n.Properties).HasForeignKey(m => m.AuditEntityId);
}
}
}
  1. 在程序包管理控制台中执行Add-Migration -Context MySqlAuditDbContext newDbContext,创建迁移脚本,因系统中存在2个DbContext,所以需要指定上下文-Context MySqlAuditDbContext

  2. 在程序包管理控制台中执行update-database -Context MySqlAuditDbContext

  3. 至此,数据库中新生成了一个库,库中包含用于审计功能的三张表

OsharpNS轻量级.net core快速开发框架简明入门教程-多上下文配置(多个数据库的使用)的更多相关文章

  1. OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Redis使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  2. OsharpNS轻量级.net core快速开发框架简明入门教程-从零开始启动Osharp

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  3. OsharpNS轻量级.net core快速开发框架简明入门教程-代码生成器的使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  4. OsharpNS轻量级.net core快速开发框架简明入门教程-基于Osharp实现自己的业务功能

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  5. OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Hangfire使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  6. OsharpNS轻量级.net core快速开发框架简明入门教程-Osharp.Permissions使用

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  7. OsharpNS轻量级.net core快速开发框架简明入门教程-切换数据库(从SqlServer改为MySql)

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  8. 【开源】OSharpNS,轻量级.net core快速开发框架发布

    OSharpNS简介 OSharp Framework with .NetStandard2.0(OSharpNS)是OSharp的以.NetStandard2.0为目标框架,在AspNetCore的 ...

  9. [开源]OSharpNS - .net core 快速开发框架 - 快速开始

    什么是OSharp OSharpNS全称OSharp Framework with .NetStandard2.0,是一个基于.NetStandard2.0开发的一个.NetCore快速开发框架.这个 ...

随机推荐

  1. 如何使用gitlab自建golang基础库

    这里以go mod方式建立golang基础库 一.gitlab创建项目golib 地址为gitlab.xxx.com/base/golib 示例如下 go mod初始化命令 go mod init g ...

  2. Spring整合Mybaits java.sql.SQLException: Access denied for user '***'@'localhost' (using password: YES)

    最近在搞Spring和Mybatis的整合,当我们在Spring里面配置数据源,而数据源是从外部的properties文件读取过来的时候就会报错 java.sql.SQLException: Acce ...

  3. PIE调用Python获得彩色直方图

    前段时间我一直在研究PIE SDK与Python的结合,因为在我的开发中,我想获取一张图片的统计直方图,虽然在SDK中有提供关于直方图的类接口(如IStatsHistogram 接口.Histogra ...

  4. mvc5 源码解析1:UrlRoutingModule

    注册在C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG \webconfig中 在该module源码中 我们可以看出注册了application ...

  5. Vertx上传 官网Demo Java版

    package io.vertx.example.web.upload; import io.vertx.core.AbstractVerticle; import io.vertx.example. ...

  6. Python collectioins

    collections是一个python的内建模块,提供了一些除了dict.list.tuble.等常见的数据类型之外的一些集合类 参考链接:https://www.liaoxuefeng.com/w ...

  7. 安卓微信对接H5微信支付出现“商家参数有误,请联系商家解决”的问题处理

    最近遇到客户在对接我们微信支付的时候,一些商家反馈在用户支付的过程中会出现报错,出错的截图如下: 查看微信官方文档如下:https://pay.weixin.qq.com/wiki/doc/api/H ...

  8. SharpGL之透视投影和摄像机

    当三维体放在世界坐标系中后,由于显示器只能用二维图像显示三维休,因此必须要依赖投影来把三维体降低维数. 投影变换的目的就是定义了一个视景体,使得视景体外多余的部分不会显示. 投影包括透视投影(pers ...

  9. hashlib以及hmac的日常应用

    首先我们要明白日常的Web网页为了保证数据的安全都会对数据的存储进行一些加密,当我需要在此网站验证时就需要对原有的密码以及账号进行加密此时我们就需要用到hashlib这个框架的一些功能,下面我就来更大 ...

  10. nginx Linux内核参数的优化

    默认的Linux内核参数考虑的是最通用的场景,这明显不符合用于支持高并发访问的Web服务器的定义,所以需要修改Linux内核参数,使得Nginx可以拥有更高的性能. 这里针对最通用的.使Nginx支持 ...