c# - How write a integration Test in Entity Framework to Test if data added to 2 related tables? -
i writing integration test in c# db taking care entity framework6. have problem test if data added correctly when data added 2 tables related key.
consider:
make table - id, name, yearid year table - id, calenderyear relationship:
1 many - 1 year has n make this method in integration test test if can add both table:
[testmethod] [testcategory("integration")] public void applicationscontext_addmake_shouldaddsuccessfully() { // arrange. var currententity = applicationscontext.makes.tolist<make>(); // act. var newyear = fakes.getnewyear(); applicationscontext.years.add(newyear); applicationscontext.savechanges(); var newmake = fakes.getnewmake(); applicationscontext.makes.add(newmake); applicationscontext.savechanges(); // assert. var entityafteroperation = applicationscontext.makes.tolist<make>(); assert.istrue(currententity.count < entityafteroperation.count); } this fake class:
public static year getnewyear() { var newyear = new year() { calendaryear = 2015 }; return newyear; } public static make getnewmake() { var newmake = new make() { name = "toyota", description = "this make description", }; return newmake; } this not working don'y know how send id of year make? error getting:
{"violation of unique key constraint 'uq_year_calendaryear'. cannot insert duplicate key in object 'common.year'. duplicate key value (2015).\r\nthe statement has been terminated."}
you're not relating year , make you're creating when add them database.
so before add them, should add newmake newyear.
var newyear = fakes.getnewyear(); newyear.makes.add(fakes.getnewmake()); applicationscontext.years.add(newyear); applicationscontext.savechanges(); when before saving changes, both added database in 1 shot.
Comments
Post a Comment