

In conclusion, I think SQLite can be a very powerful tool in our toolbox. Next, I will run the test, and it is green. Var provider = new EmployeeProvider(context) Īssert.AreEqual("John", employee.FirstName) Using (var context = new EmployeeContext(options)) Var employee = employeeProvider.Get(1002) Ĭonsole.WriteLine($"Name: ) Var employeeProvider = new EmployeeProvider(employeeContext) Var employeeContext = new Security Info = False Integrated Security = true Initial Catalog = TimeManagement server = Nirjhar-Tab\SQLEXPRESS") And in the Get method use it to return an Employee matching the id. The implementation of class EmployeeProvider is to take EmployeeContext as a constructor parameter. And class EmployeeProvider will contain the implementation. The interface IEmployeeProvider is the contract for accessing an Employee by id. Next, I will create an interface and class for accessing the Employee. OptionsBuilder.UseSqlServer(connectionString) Protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) Public EmployeeContext(string connectionString) Private readonly string connectionString And Overriding OnConfiguring method to setup Entity Framework to use SQL Server. It is taking connection string in the constructor. The implementation of EmployeeContext is straight forward.

This is needed, as the column names in the database are different from the property names of the class. Hence, I will create a Employee data entity class as well.įor the Employee class, I will annotate the properties with the Column attribute from namespace. This class will return all Employee information from database. Now, I will create a class EmployeeContext deriving from DbContext of EntityFrameworkCore, to create my data access class.

And during testing, I will use the same table structure in the SQLite database.įor this example, I will read from the Employee table. The DatabaseĪs mentioned in the setup, I will connect to a SQL Server Express and access a table.
#Open sqlite database install#
Note: will install Microsoft.EntityFrameworkCore as a dependency.

I will install 2.2.6 version.Īdditionally, I will install the following NuGet packages to the Unit Test Class library project. Secondly, I will add the EntityFrameworkCore.SqlServer to the SqliteDemo console application. Once the Nuget package manager window pops up, in the Browse tab search for EntityFrameworkCore. Therefore, right-click on the project and select Manage Nuget Packages. Adding Nuget Packageįirstly, I will add the Entity Framework NuGet package to the SqliteDemo console application. For that, I will add a new Class Library project to the solution. Secondly, I will create a Test application. This will create a new ASP.Net Core console application. Net Core -> ASP.NET Core Console Application. Net Core Console Applicationįirstly, I will open Visual Studio 2017, go to File -> New -> Project. Secondly, I will create a unit testing project with NUnit, which will use SQLite in-memory for unit testing the database components. Which will connect to SQL Server express using Entity Framework. It is built into all mobile phones, which I did not know before researching for this blog post. SQLite is the most used database engine in the World. It is a small, fast, self-contained and highly reliable database engine. SQLite is a fully-featured SQL database engine. I have been receiving requests from a few of you to create a post and YouTube video on unit testing with SQLite and. Hello everyone, and welcome back to dotnetcorecentral.
