.net framework(4.6.2) 迁移 .net core(2.2) 总结
Posted on 2019-12-26 22:42 Hamilton Tan 阅读(517) 评论(0) 编辑 收藏 举报1.创建EF的类库,同时将此项目设置为启动项(为Scaffold-DbContext -tables指令使用),同时安装2个包
1).Microsoft.EntityFrameworkCore.Tools
2).Pomelo.EntityFrameworkCore.MySql (这个是第三方的ef mysql 中间件)
Scaffold-DbContext -Force "Server=127.0.0.1;User Id=root;Password=123456;Database=test" -Provider "Pomelo.EntityFrameworkCore.MySql" -UseDatabaseNames -OutputDir DataModels -ContextDir DataModels -Context TESTDbContext
参考:https://www.cnblogs.com/DNLi/p/9524976.html
https://www.cnblogs.com/adandelion/p/10529788.html
2.实现HttpContext
需要引用:using Microsoft.AspNetCore.Http;
/// <summary> /// 自定义执行上下文 /// </summary> public static class HttpContextCustomize { private static IHttpContextAccessor _httpContextAccessor; public static void Configure(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; } public static HttpContext Current { get { return _httpContextAccessor.HttpContext; } } }
调用: HttpContextCustomize.Current
3.处理web.config配置文件
1).首先在解决方案中Nuget 引入System.Configuration.ConfigurationManager
2).导入web.config文件到项目根目录,并将名称修改为app.config. 因为.NET Core的项目本质是控制台应用
3).去除config中和需要的配置无关的内容,主要是<system.web>,<system.webServer>和<system.codedom>等典型asp.net标签
参考:https://www.cnblogs.com/mantgh/p/7425113.html
4.MemoryCache问题修复
添加对Microsoft.Extensions.Caching.Memory命名空间的引用,它提供了.NET Core默认实现的MemoryCache类,以及全新的内存缓存API
参考:https://www.jb51.net/article/122190.htm
5.如何使用.net framwork 的Unity(Microsoft.Practices.Unity)?
在Startup.cs添加 services.AddScoped<IUnitOfWork, UnitOfWork>();
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddScoped<IUnitOfWork, UnitOfWork>();
}
参考:https://stackoverflow.com/questions/53562269/unity-framework-in-net-core
6.[FromQuery] 替换 [FromUri]