Now many people will say that this shouldn’t be done but I really need to test using the actual DB.


Edit: I’ve resolved this. I just placed the Tests in the project folder I am testing.
This is more of an integration test / end to end test than a unit test.

Initialize the objects you need in your unit test initialization. Make sure the database is running while you run the tests.
https://khalidabuhakmeh.com/dotnet-database-integration-tests

If you are running SQL, you can unit test it directly,
https://www.sqlservercentral.com/articles/sql-server-unit-testing-in-visual-studio
As the author of the blog post, I recommend not getting hung up on whether it is a "unit test" or an "integration test". I like to focus on the value of the test. Considering EF query syntax has a lot of edge cases, it’s best not to assume anything and just hit a database.
Also, I recommend using the same database engine you’ll be using in your production environment. Do not use SQLite if your target is a SQL Server variant. You’ll save yourself a lot of headaches.
Finally, you can speed up query tests by sharing database instances across tests via XUnit’s TestFixture implementation. If you’ll be changing the state of the database you might want a fresh instance each time.
Take all advice with a healthy dose of “it depends”.
Cheers.
Hello, how do I test EF, by using my local DB? Yes it is more of an integration test
It’s not that it shouldn’t be done, it’s just that this would be integration testing rather than unit testing, but there’s nothing wrong with it.
What is the problem exactly? Integration tests are pretty straightforward, you setup a testing database and then you automate what you would check manually. Even if they can’t be considered unit tests you can use a unit testing framework and manage the tests in a similar way.
You don’t. You do an Integration Test.


I think it should be done. The approach I use is to drop and recreate the database and seed it with test data.
I do this for each test or (preferably) a suite of tests that I know do not depend on each other.
I wrote a couple utilities for this. See here.
What problem are you having exactly? Surely just reset the database to a known state and point your tests at it.
C# devs
null reference exceptions

source