So I am Visual Studio 2017, EF6 and I am using this T4 template https://marketplace.visualstudio.com/items?itemName=SimonHughes.EntityFrameworkReversePOCOGenerator to generate my context class
Being a big fan of testing I already have 100% coverage with unit tests, but now I want to do Integration tests where I can call multiple parts of my system and pass through the changes - but of course I want to do this without touching a real disk DB.
So after searching for a bit I found Effort https://entityframework-effort.net/ and to me this looks like exactly what I want to use. Where I can create a database thats in memory, fill it with whatever fixtures I want and than call multiple methods from various parts of my system. However, the thing I am trying to find out is how to make it work in my situation I have tried DBConnectionFactory, EntityConnectionFactory, ObjectConnectionFactory and every time I get the Error Message:-
"EffortException: Database has not been initialized".
this is the code block I am using in my setup:-
var newsCtx = Effort.DbConnectionFactory.CreatePersistent("2");
_newsEntities = new NewsEntities(newsCtx);
_newsEntities.Database.CreateIfNotExists();
_newsEntities.Channels.Add(new Channel
{
Id = 1,
Title = "Convenience Store - ",
Link = "https://www.conveniencestore.co.uk/XmlServers/navsectionRSS.aspx?navsectioncode=123",
Description = "https://www.conveniencestore.co.uk",
Image = "https://www.conveniencestore.co.uk/magazine/dest/graphics/logo/logo.png",
PublishedAt = null,
UpdatedAt = DateTime.Now.AddDays(-2),
Enabled = true,
Type = "Rss_2_0",
Author = null,
Category = null,
Copyright = null,
Generator = null
});
_newsEntities.SaveChanges();
When I get to the saveChanges I get the exception which suggests that I add the .CreateIfNotExists() which I have already done, however, I still get the error. I can see samples and examples on various links where it works... however I dont want to abandon my t4 template as it gives me exactly what I want.
I am a little lost and I am wondering has anybody used a reverse poco generator that creates the context and used this in conjunction with Effort. I would love to use both and If somebody can give me a pointer so that I can get decent integration tests working with in memory DB I would be as happy as a sandboy
Regards Julian
this is the generated method in the Poco from the t4 template
public NewsEntities(System.Data.Common.DbConnection existingConnection)
: base(existingConnection, true)
{
this.Database.Connection.Open();
}
Ok, so after reviewing https://www.stevefenton.co.uk/2015/11/using-an-in-memory-database-as-a-test-double-with-entity-framework/ this link, I got to wondering about how my reverse poco generator was behaving in the methods I had the following inside the method
this.Database.Connection.Open();
this was causing the problem.... removing this line from the .ttinclude poco generator enabled Effort to run. Why this was there I have no idea, and removing it doesnt seem to have an effect.
Therefore, I am happy as a sand boy again and I can continue with my integration tests using an in memory database. If you come across this problem see if somebody has modified your t4 template to do something different.