using NHibernate; using NHibernate.Cfg; // Create configuration object var configuration = new Configuration(); configuration.Configure(); // Build session factory var sessionFactory = configuration.BuildSessionFactory(); // Get session using (var session = sessionFactory.OpenSession()) { // Perform database operations here }
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; // Add DbContext to services container var services = new ServiceCollection() .AddDbContextIn both examples, we first create a session factory object by either configuring NHibernate or registering a DbContext with Entity Framework Core. We then use this factory object to obtain a session object, which we can then use to interact with the database. Package library: NHibernate and Microsoft.EntityFrameworkCore, respectively.(options => options.UseSqlServer(connectionString)) .BuildServiceProvider(); // Get session using (var scope = services.CreateScope()) { var dbContext = scope.ServiceProvider.GetService (); // Perform database operations here }