Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effort unit testing Entity framework 6.1.3 DB-first

I am experiencing problems when using Effort framework (version 1.1.4) to unit test my DB-layer.

I have a DB-layer using Entity framework 6.1.3 and the model is created using database-first approach so there is an *.edmx file describing the model.

I have created a partial class to expose an additional constructor used by the unit tests with effort like this:

public partial class Entities
{
    public Entities(DbConnection connection)
        : base(connection, true)
    {
    }
}

The simple unit-test looks like this:

    private Entities CreateContext()
    {
        //var connectionString = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString;
        //var connection = Effort.EntityConnectionFactory.CreateTransient(connectionString);
        //return new Entities(connection as DbConnection);

        var connection = Effort.EntityConnectionFactory.CreatePersistent("name=Entities");
        var context = new Entities(connection);
        return context;
    }

    [TestMethod]
    public void Testing_Effort_Integration()
    {
        using (var context = CreateContext())
        {
            var entity = context.TableEntity.FirstOrDefault(i=> i.Id);
            Assert.IsNotNull(entity);
        }
    }

When I run the unit test it throws an exception for the line:

var connection  = Effort.EntityConnectionFactory.CreatePersistent("name=Entities");

{"The provider did not return a ProviderManifest instance."} InnerException Message: {"Could not determine storage version; a valid storage connection or a version hint is required."}

Other posts I have found suggests to change the ProviderManifestToken attribute in the *.edmx file from "2012" to "2008". This seems to solve the problem but instead gives my another exception when trying to use the context for the first time here:

var entity = context.TableEntity.FirstOrDefault(i=> i.Id);

NotSupportedException Unable to determine the provider name for provider factory of type 'System.Data.EntityClient.EntityProviderFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.

Anyone know how to solve this issue so I can use Effort with entity framework 6.1.3 DB-first approach?

I have successfully been able to use Effort (version 1.1.4) to unit test DB-layers created in EF 4 and EF 5 with DB-first approach - this is why I think that the EF-version can be of interest...

like image 271
gurkan Avatar asked Jan 12 '16 12:01

gurkan


1 Answers

A colleague of mine found the solution to my issue!

Apparently I was using the "Effort" nuget package instead of the "Effort.EF6" nuget package. After uninstalling and installing the other I also had to update my App.Config with the tags:

  <system.data>
    <DbProviderFactories>
      <add name="Effort.Provider" invariant="Effort.Provider" description="Effort.Provider" type="Effort.Provider.EffortProviderFactory,Effort" />  
    </DbProviderFactories>
  </system.data>

  <entityFramework>
    <providers>
      <provider invariantName="Effort.Provider" type="Effort.Provider.EffortProviderServices, Effort" />
    </providers>
  </entityFramework>

And I also included a call in the SetUp for my unit tests to register the effort provider:

    [SetUp]
    public void Setup()
    {
        EffortProviderConfiguration.RegisterProvider();
    }

This solved the issue for my. Hopefully it can provide some help to others!

like image 148
gurkan Avatar answered Sep 30 '22 19:09

gurkan