2

I need to implement testing for some applications, but I'm not sure what would be the best and cleanest way to implement the tests in a way that it is easily maintainable. The scenario is as follows:

I have multiple applications, including a MVC .Net Framework project, and multiple Azure functions, one implemented with .Net Framework cause some dependencies need it, but most of them in .Net Core. All those applications uses EntityFramework to operate a MSSQL database, and I need to find a way to seed an in memory or temporary database with default configuration data and some pre-made records before testing.

One solution I thought of is to make a Class Library project that seeds the database, and every testing project can reference that project, and when something needs to be added to the seed you just have to edit that project. But I don't know if this is the best solution, because I have .Net Framework and .Net Core apps, so probably I need at least 2 implementations of each method for each of the EntityFramework versions, and I'm not sure if in this way my seeder class needs to know the DB schema or I can implement code based on DBContext class for example.

Would this be a good solution? Is there any other standard way of implementing something like this?

1 Answer 1

1

There's alot to assume from your question: the way I understand it, is you would pass the relevant DbContext to your seeder class anyway. Assuming by 'testing', you mean just putting appropriate test data into a SQL database, then a .NET Core Class Library where your seed class exists is enough; it just needs to know about the DbContext.

Example seeder class:

using System.Linq;
using System.Threading.Tasks;

using BusinessLogic.Entities;  //this is where your business/EF entities live

namespace DataAccessLayer.Data.Services
{
    public class SeederService
    {
        #region FIELDS
        private readonly TestContext _passedInDbContext;

        #endregion

        #region CONSTRUCTOR
        public SeederService(TestContext passedInDbContext)
        {
            _passedInDbContext = passedInDbContext;
        }

        #endregion

        #region METHODS PUBLIC
        public async Task<bool> SeedDatabaseAsync()
        {
            if (!_passedInDbContext.Currencies.Any()) //see if any entries already exist in the Currency SQL table
            {
                //test data
                _passedInDbContext.Currencies.AddRange
                    (
                        new Currency("GBP", "GBP United Kingdom Pounds", "£", 1.000m),
                        new Currency("USD", "USD United States Dollar", "$", 1.200m),
                        new Currency("EUR", "EUR Euro", "€", 1.100m)
                    );
            }

            await _passedInDbContext.SaveChangesAsync();

            return true;
        }
        #endregion
    }
}

Hope this helps somewhat. Of course, you would want to set up a test SQL database and migrate/update/seed this database with the appropriate connection string.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah that is basically what I was thinking. But if I write a .Net Core Class Lib, can I reference it in a project made in .Net Framework? And about passing the context to the lib, if I have a context from EntityFramework, will it work with the EntityFrameworkCore implementation inside the Class Lib?
Do all projects/solutions reference the same SQL database and with same/similar connection strings?
Yeah, exactly same database, same connection string, some projects only have EF references to some tables from the database, some have to all tables, but it is the same DB.
You would ideally have something like a DataAccessLayer which holds the context for all your projects, which can be referenced. You can't reference a .NET Core class library from a .NET Framework project. So, create a class library in .NET Framework 4.8 (for example), which holds your context, and where you can place your seeding method, and all your other projects can reference this. Let me check on how to mix different EF versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.