简体   繁体   中英

Unit/integration testing EF with Effort - Connectionstring not found

I recently started using the in-memory database Effort to unit test my entity framework implementations.

Everything works well, except when I try to load data from a CSV file into the database using the CSV dataloader.

Documentation on exactly how to configure the csvdataloader seems to be scarce / insufficient.

Here is one of the NUnit 3.0 unit tests I have written:

    [Test]
    public void GetBudgetIncomeItems_MultipleValidItems_ReturnList()
    {
        //Prepare and load data
        var dataLoader = new CsvDataLoader(@"<DirectoryOfUnitTestProject>\Data");
        var entityConnection = Effort.EntityConnectionFactory.CreateTransient("name=<NameOfMyCSVFileWithoutCSVExtension/DatabaseName>", dataLoader);
        _serviceContext = new ServiceContext(entityConnection);

        //Execute test
        var items = _query.GetBudgetIncomeItems(new Application {Id = 3});

        //Check results
        //Some assertions
    }

An exception is thrown on the line:

Effort.EntityConnectionFactory.CreateTransient("name=<NameOfMyCSVFileWithoutCSVExtension/DatabaseName>", dataLoader);

Exception detail:

System.ArgumentException was unhandled by user code
HResult=-2147024809 Message=Connectionstring was not found Parameter name: entityConnectionString ParamName=entityConnectionString
Source=Effort StackTrace: at Effort.EntityConnectionFactory.GetFullEntityConnectionString(String entityConnectionString) at Effort.EntityConnectionFactory.GetEffortCompatibleMetadataWorkspace(String& entityConnectionString) at Effort.EntityConnectionFactory.CreateTransient(String entityConnectionString, IDataLoader dataLoader) at Infrastructure.EF6.UnitTests.PacsQueryTests`1.GetBudgetIncomeItems_MultipleValidItems_ReturnList() in \\PacsQueryTests.cs:line 36 InnerException:

Any help would be appreciated...

Update

As per comments from @Evk below, when using Code first, you have to make use of the DbConnection and DbConnectionFactory classes, and not the EntityConnection and EntityConnectionFactory classes.

My updated code, which does not throw any errors, looks as follows:

        [Test]
        public void GetBudgetIncomeItems_MultipleValidItems_ReturnList()
        {
            //Prepare and load data
            Effort.DataLoaders.IDataLoader csvDataLoader = new Effort.DataLoaders.CsvDataLoader(@"<DirectoryOfUnitTestProject>\Data");
            var dataLoader = new Effort.DataLoaders.CachingDataLoader(csvDataLoader,false);
            DbConnection dbConnection = Effort.DbConnectionFactory.CreateTransient(dataLoader);
            _serviceContext = new ServiceContext(dbConnection);

            //Execute test
            var items = _query.GetBudgetIncomeItems(new Application {Id = 3});

            //Check results
            //Some assertions
        }

I know I am late to this party but their is one post that seems to answer the question. I spent half a day looking for it. Hopefully posting it here will spread the word.

https://github.com/zzzprojects/EntityFramework-Effort/issues/99

Credit goes to the author of the original post above.

EntityFrameworkEffortManager.ContextFactory = context => new MyContext();
var dataLoader = new EntityDataLoader();
DbConnection dbConnection = DbConnectionFactory.CreateTransient(dataLoader);
var effortContext = new MyContext(dbConnection);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM