c# - NullReferenceException when nothing is null? -
i testing (integration) repository layer , keep getting null reference error ef. concern is, there nothing in object null when run debugger. in fact there nothing in entire method null. of properties set in automapper , using real (not mocked) values everything. there , use real version of code.
test:
[test] public void clone_shouldcloneuser() { using (var scope = new transactionscope()) { //arrange var request = builder<cloneuserrequest>.createnew() .with(x => x.keytoclone = 29) .with(x => x.user = builder<user>.createnew().with(y => y.key = 0) .build()) .build(); //act _sut.clone(request); //assert assert.doesnotthrow(() => _sut.clone(request)); } }
method:
public void clone(cloneuserrequest request) { var usersgroupstobecloned = _context.webusergroups.where(x => x.userkey == request.keytoclone).tolist(); var webuser = _mappingservice.map(request.user, new webuser()); webuser.webusergroups = usersgroupstobecloned; //on line receive nullreferenceexception, nothing null _context.webusers.add(webuser); _context.savechanges(); }
stack trace:
system.nullreferenceexception : object reference not set instance of object. @ system.data.entity.core.objects.dataclasses.relatedend.markforeignkeypropertiesmodified() @ system.data.entity.core.objects.dataclasses.entityreference.addtonavigationpropertyifcompatible(relatedend otherrelatedend) @ system.data.entity.core.objects.dataclasses.relatedend.includeentity(ientitywrapper wrappedentity, boolean addrelationshipasunchanged, boolean doattach) @ system.data.entity.core.objects.dataclasses.entitycollection`1.include(boolean addrelationshipasunchanged, boolean doattach) @ system.data.entity.core.objects.dataclasses.relationshipmanager.addrelatedentitiestoobjectstatemanager(boolean doattach) @ system.data.entity.core.objects.objectcontext.addobject(string entitysetname, object entity) @ system.data.entity.internal.linq.internalset`1.<>c__displayclassd.<add>b__c() @ system.data.entity.internal.linq.internalset`1.actonset(action action, entitystate newstate, object entity, string methodname) @ system.data.entity.internal.linq.internalset`1.add(object entity) @ system.data.entity.dbset`1.add(tentity entity) @ repositories.userrepository.clone(cloneuserrequest request) in userrepository.cs: line 39 @ repositories.userrepositoryintegrationtests.clone_shouldcloneuser() in userrepositoryintegrationtests.cs: line 50
is there obvious missing or bug in ef perhaps?
i figured out wrong. our db structure terrible , there circular reference between webusergroup (it has child webuser) , webuser(it has collection of webusergroups). when got of usergroups clone user, had reference user cloning. somehow ef decided send me nullreferenceexception. when changed webuser property on groups new 1 works great. time talk dba's updating this...
Comments
Post a Comment