Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading related data by foreign key in mocked EF6 database

Recently I've started a project for simulating various neural network applications. To handle the considerable amount of data involved I've implemented a SQL-Server back end using the Entity Framework Model first approach.
Additionally I've grown quite fond of the test driven development (TDD) discipline. However in order to do so I need a mock database to run my tests on. After much searching I found multiple solutions both commercial and open source. At this point it's worth noting that I'm quite the novice when it comes to Database related subjects. Consequently I elected to use the msdn described in memory mock for the EF6 framework as described here.

Pff, now we got the background out of the way, here is my question:

I've created my model and generated the database from it. The code generation of the real interface layer is done by the edmx. To unit test I've followed the above described tutorial and adapted the relevant fields to match my database model,. (which can be found http://i.imgur.com/julse1Y.png?1, unfortunately I dont have the rep to include the image in the post...).
However I don't need all the data for every test, only some subsets. I would like a situation where i can specify in the setup of the test which tables to load an perhaps the depth of loading the dependencies.

The problem I encountered was that the related data was not loaded along with current DBSet when specifying the related foreign key. For example when setting up the data for the Connection set I would like the entries to link to the related data, in this case elements of the Node set:

var context = new TestContext();

context.NodeSet.Add(new Node { NodeID = 1 });
context.NodeSet.Add(new Node { NodeID = 2 });

context.ConnectionSet.Add(new Connection { ConnID = 1, ToNodeID = 1, FromNodeID = 2 });

var conn = context.ConnectionSet.First();
var fromNode = conn.FromNode;
var toNode = conn.ToNode;

In this example the FromNode property of the specified Connection returns null. The desired situation is that the mock has the ability to relate the ToNodeID key to the relevant object. Furthermore I expect that the order of loading is relevant for resolving the key constraints i.a.w. in order to resolve the FromNode field the corresponding Node must exist.

I am open to all suggestions regarding this specific question but also if any of you know a more constructive way to mock an EF6 model first database design.

Thanks in advance!

like image 596
Bermudian Avatar asked Nov 10 '22 13:11

Bermudian


1 Answers

After the discussion and rethinking the issue, I believe the easiest approach would be to forget about automatic resolving by ID but rather manually set up references prior to your tests.

In other words, if you have two dbsets, Child and Parent and each child entity points to a single parent so that you have

public class Child
{
   public int ID_PARENT { get; set; }
   public virtual Parent Parent { get; set; }
}

then you set up your mocks with data

var mockedContext = ... ; // create mocked context with mocked DbSets as
                          // shown in the article you refer to

Child c = new Child();

Parent p = new Parent();
c.Parent = p;  // set up the relation manually

mockedContext.Child.Add( c );
mockedContext.Parent.Add( p );

// property resolving works now as you have set it up in an explicit way
// note that IDs are irrelevant
var children = mockedContext.Child.Where( c => c.Parent != null && c.Parent.Name.StartsWith( "n" ) );
like image 85
Wiktor Zychla Avatar answered Nov 15 '22 10:11

Wiktor Zychla