.net core 6.0使用记录一

发布时间:2021-12-23 18:32:27来源:本站阅读(767)

    今天用了一天,不断学习吧,改变还是挺大的。

    首先说EFCORE,enable-migrations 命令居然没了。会提示

    Enable-Migrations is obsolete. Use Add-Migration to start using Migrations.

    好吧,既然让用 Add-Migration 咱也就不纠结了。还省一步操作。。。

    官方给出的应用迁移代码

    public static void Main(string[] args)
    {
    var host = CreateHostBuilder(args).Build();

    using (var scope = host.Services.CreateScope())
    {
    var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
    db.Database.Migrate();
    }

    host.Run();
    }

    对了,.net core依然需要注意引包 ,没有 Microsoft.EntityFrameworkCore.Tools 是使用不了迁移命令的。。。

    其实再说下代码,没有了 Startup.cs ,直接使用了 Program.cs

    看下Program.cs的代码 

    var builder = WebApplication.CreateBuilder(args);

    // Add services to the container.

    builder.Services.AddControllers();
    // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();

    builder.Services.AddDbContextPool<DaoContext>(o => o.UseSqlServer(builder.Configuration.GetConnectionString("SqlServer")));

    var app = builder.Build();



    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
    app.UseSwagger();
    app.UseSwaggerUI();
    }

    app.UseAuthorization();

    app.MapControllers();

    using (var scope = app.Services.CreateScope())
    {
    var db = scope.ServiceProvider.GetRequiredService<DaoContext>();
    db.Database.Migrate();
    }

    app.Run();

    满满的简洁感,以上还有我自己加的。

    总体感觉不错。配合VS2022使用,很nice。。。


关键字net core 6 ef core 6