首先仔细阅读官方给出的升级文档这是地址
简介 - Training | Microsoft Learn
跟据文档中的操作升级
升级之后可能会有大量报错,将报错都改好,运行
如果能正常运行起来那么恭喜你,一般是会有问题
我遇到的问题是项目启动不了,各种报错,改好错误之后能顺利生成了,看一下你的vs2022 是不是最新,更新到最新。
然后我贴出Program.cs 文件,供大家参考
using Microsoft.EntityFrameworkCore;
using XinHua2021.Models;
using YuanFuTouBao2022Core.Dao;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
//注入Session
builder.Services.AddSession();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSession();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Login}/{action=Index}/{id?}");
app.Run();
直接启动运行,这时候项目就能启动了,再改一下js引用css引用,改一下数据库链接这里放出sqlserver 和 mysql 两种方式的数据库链接
首先要引入包
项目应该有这些包
sqlserver:
namespace XinHua2021.Models
{
using Microsoft.EntityFrameworkCore;
using NPOI.SS.Formula.Functions;
using DbContext = Microsoft.EntityFrameworkCore.DbContext;
public partial class mssql0081881Entities : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connString = @"Data Source=.;Initial Catalog=aa;Persist Security Info=True;User ID=a;Password=a;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;";//数据库连接字符串
optionsBuilder.UseSqlServer(connString);
}
public virtual DbSet<YFTB_CfmMessages> YFTB_CfmMessages { get; set; }
public virtual DbSet<YFTB_Config> YFTB_Config { get; set; }
}
}
mysql :建一个类
using Microsoft.EntityFrameworkCore;
using System.Data.Entity;
using DbContext = Microsoft.EntityFrameworkCore.DbContext;
namespace YuanFuTouBao2022Core.Dao
{
public class SqlContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
string connectStr = "server=.;port=3306;database=a;uid=a;pwd=a;CharSet=utf8";
//注意了,下面的那么Version是mysql的版本号,8.0.3是我的版本号,你可以通过在cmd中输入
//mysql --help查看自己的版本号
optionsBuilder.UseMySql
(connectStr, new MySqlServerVersion(new Version(8, 0, 29)));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
}
public Microsoft.EntityFrameworkCore.DbSet<YFTB_LoginUser> YFTB_LoginUser { get; set; }
}
}
基础类建好,分别把实体类在补充上 数据库这块就搞定了
在controller中就可以使用了文章来源:https://www.toymoban.com/news/detail-803908.html
public class LoginController : Controller
{
mssql0081881Entities db = new mssql0081881Entities();
// GET: Login
SqlContext sqlContext = new SqlContext();
public IActionResult Index()
{
var a = sqlContext.YFTB_LoginUser.ToList();
return View();
}
}
再去修改 项目中controller和views具体的写法就好了。文章来源地址https://www.toymoban.com/news/detail-803908.html
到了这里,关于asp.net mvc framework 4.8 升级到 net 8.0的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!