{"id":20605195,"url":"https://github.com/arnab-developer/efunittest","last_synced_at":"2026-04-20T11:04:18.141Z","repository":{"id":68915178,"uuid":"250462084","full_name":"Arnab-Developer/EfUnitTest","owner":"Arnab-Developer","description":"Unit test of Entity Framework Core data access layer.","archived":false,"fork":false,"pushed_at":"2021-04-09T11:55:16.000Z","size":679,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-17T02:24:16.780Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Arnab-Developer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-27T06:52:47.000Z","updated_at":"2021-12-05T05:48:17.000Z","dependencies_parsed_at":"2023-06-18T23:06:50.002Z","dependency_job_id":null,"html_url":"https://github.com/Arnab-Developer/EfUnitTest","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2FEfUnitTest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2FEfUnitTest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2FEfUnitTest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2FEfUnitTest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Arnab-Developer","download_url":"https://codeload.github.com/Arnab-Developer/EfUnitTest/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242250932,"owners_count":20096895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T09:26:59.539Z","updated_at":"2026-04-20T11:04:18.095Z","avatar_url":"https://github.com/Arnab-Developer.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EF unit test\n\nUnit test of Entity Framework Core data access layer.\n\nAn example of data access layer unit testing with Entity Framework Core. In the application Entity Framework Core is using Sql Server provider to save and read data. But in unit testing, Entity Framework Core is using memory provider to test the data access layer.\n\nWe can use Entity Framework Core db context for our data access layer. Data access layer can communicate with real external data source like SQL Server. But the question is how we can unit test the data access layer? Because during unit testing it is not possible to work with real database.\n\n\nThe process to unit test a data access layer which is using Entity Framework Core db context is using in-memory provider. Entity Framework Core has different providers. We can push provider to Entity Framework Core db context class in runtime. When the application is running we can push Entity Framework Core SQL Server provider to work with real SQL Server database and at the time of unit testing we can push in-memory provider to test with in memory data.\n\nTo show you a demo, I have created one small application.\n\nIt is a MVC application with ASP.NET Core and Entity Framework Core. In backend I have used SQL Server.\n\nModel\n\n```c#\npublic class Student\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n    public string Subject { get; set; }\n}\n```\n    \nDb context class\n\n```c#\npublic class StudentContext : DbContext\n{\n    public StudentContext(DbContextOptions\u003cStudentContext\u003e options)\n        : base(options)\n    {\n    }\n\n    public DbSet\u003cStudent\u003e Students { get; set; }\n}\n```\n\nI have added the db context class in startup for dependency injection. Please notice that I have pushed SQL Server provider in the db context here.\n\n```c#\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddControllersWithViews();\n    var conStr = Configuration.GetValue(\"ConStr\");\n    services.AddDbContext\u003cStudentContext\u003e(option =\u003e option.UseSqlServer(conStr));\n}\n```\n    \nI have added the connection string in appsettings.json.\n\n```json\n{\n  \"Logging\": {\n    \"LogLevel\": {\n      \"Default\": \"Information\",\n      \"Microsoft\": \"Warning\",\n      \"Microsoft.Hosting.Lifetime\": \"Information\"\n     }\n   },\n   \"AllowedHosts\": \"*\",\n   \"ConStr\": \"Data Source=(localdb)\\\\MSSQLLocalDB;Initial Catalog=StudentDb;Integrated Security=True\"\n}\n```\n    \nController\n\n```c#\npublic class StudentController : Controller\n{\n    private readonly StudentContext _studentContext;\n\n    public StudentController(StudentContext studentContext)\n    {\n        _studentContext = studentContext;\n    }\n\n    public IActionResult Index()\n    {\n        var students = _studentContext.Students\n            .OrderBy(student =\u003e student.Id)\n            .ToList();\n        return View(students);\n    }\n}\n```\n    \nIn case of unit testing for the controller I have pushed memory provider in db context.\n\n```c#\nvar options = new DbContextOptionsBuilder\u003cStudentContext\u003e()\n    .UseInMemoryDatabase(\"ExamTestDatabase\")\n    .Options;\nvar studentContext = new StudentContext(options);\n_studentController = new StudentController(studentContext);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farnab-developer%2Fefunittest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farnab-developer%2Fefunittest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farnab-developer%2Fefunittest/lists"}