Friday 15 April 2011

c# - Navigation property fixup for unit testing with EntityFramework 6 -



c# - Navigation property fixup for unit testing with EntityFramework 6 -

after switching entity framework 6, automatic fixup navigation properties has been removed. has wreaked havoc in our unit tests, work under assumption setting 1 side of relation, automatically update other side well.

everything works fine when running code, because detectchanges method on dbcontext automatically synchronizes relationships, when unit testing prefer not having create dbcontext synchronizaton.

i found blog explaining it's possible reuse t4 template entityframework6, doesn't work spatial info types, have moved different namespace.

so solution modify entityframework 4 t4 template file, , prepare work entityframework 6. have posted answer, still know if else has improve solution this.

generally t4 template file entityframework4 can used straight entityframework6 also, generate poco objects relationship fixup. when using spatial info types, couple of modifications necessary:

replace utility file include ef6 utilities. import right spatial name space (and others well):

<#@ include file="ef6.utility.cs.ttinclude"#>

now create ef4 template file work ef6 utilities:

replace ef4 metadata loader looks (separated :

metadataloader loader = new metadataloader(this); ... edmitemcollection itemcollection = loader.createedmitemcollection(inputfile);

with this:

var texttransform = dynamictexttransformation.create(this); edmitemcollection itemcollection = new edmmetadataloader(texttransform.host, texttransform.errors).createedmitemcollection(inputfile) edmitemcollection;

and add together missing argumentnotnull method @ bottom, before final #> tag:

public static void argumentnotnull<t>(t arg, string name) t : class { if (arg == null) { throw new argumentnullexception(name); } }

c# unit-testing entity-framework-6

No comments:

Post a Comment