NetCore 控制台 上下文 注入 dbcontext

public class Program
{
    private static   TestDBContext _tContext;

     
    private static void Main(string[] args)
    {
        Startup();
        var dd = _tContext.User.Take(1).ToList();
        //e(); 
    }

 
    private static void Startup()
    {
        var services = new ServiceCollection();
        var connectionString = "Data Source=192.168.3.1;User ID=sa;Password=sa;Database=dbname;Trusted_Connection=True;integrated security=false;TrustServerCertificate=True;";

        services.AddDbContext<TZCArchiveContext>(options => options.UseSqlServer(connectionString))
            .AddTransient<TestDBContext>()
            .AddOptions();
        var prov = services.BuildServiceProvider();
        _tContext = prov.GetService<TestDBContext>(); 
    }
}

 

posted @ 2023-07-25 11:19  荧屏  阅读(95)  评论(0编辑  收藏  举报