c# - Entity Framework 6 Runtime Error: "given key was not present in the dictionary" -
i writing asp.net app using ef6 code-first , table per hierarchy
i error whenever attempt meaningful dbcontext (such query, update, etc.):
{ "message": "an error has occurred.", "exceptionmessage": "an error occurred while preparing command definition. see inner exception details.", "exceptiontype": "system.data.entity.core.entitycommandcompilationexception", "stacktrace": " @ system.data.entity.core.entityclient.internal.entitycommanddefinition..ctor(dbproviderfactory storeproviderfactory, dbcommandtree commandtree, dbinterceptioncontext interceptioncontext, idbdependencyresolver resolver, bridgedatareaderfactory bridgedatareaderfactory, columnmapfactory columnmapfactory)\r\n @ system.data.entity.core.entityclient.internal.entityproviderservices.createdbcommanddefinition(dbprovidermanifest providermanifest, dbcommandtree commandtree, dbinterceptioncontext interceptioncontext)\r\n @ system.data.entity.core.objects.internal.objectqueryexecutionplanfactory.createcommanddefinition(objectcontext context, dbquerycommandtree tree)\r\n @ system.data.entity.core.objects.internal.objectqueryexecutionplanfactory.prepare(objectcontext context, dbquerycommandtree tree, type elementtype, mergeoption mergeoption, boolean streaming, span span, ienumerable`1 compiledqueryparameters, aliasgenerator aliasgenerator)\r\n @ system.data.entity.core.objects.elinq.elinqquerystate.getexecutionplan(nullable`1 formergeoption)\r\n @ system.data.entity.core.objects.objectquery`1.<>c__displayclass7.<getresults>b__6()\r\n @ system.data.entity.core.objects.objectcontext.executeintransaction[t](func`1 func, idbexecutionstrategy executionstrategy, boolean startlocaltransaction, boolean releaseconnectiononsuccess)\r\n @ system.data.entity.core.objects.objectquery`1.<>c__displayclass7.<getresults>b__5()\r\n @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.execute[tresult](func`1 operation)\r\n @ system.data.entity.core.objects.objectquery`1.getresults(nullable`1 formergeoption)\r\n @ system.data.entity.core.objects.objectquery`1.<system.collections.generic.ienumerable<t>.getenumerator>b__0()\r\n @ system.data.entity.internal.lazyenumerator`1.movenext()\r\n @ system.collections.generic.list`1..ctor(ienumerable`1 collection)\r\n @ system.linq.enumerable.tolist[tsource](ienumerable`1 source)\r\n @ sealingserver.controllers.pdfscontroller.<postpdf>d__4.movenext() in c:\users\thoma_000\documents\all code\sealingserver\sealingserver\controllers\pdfscontroller.cs:line 93\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.threading.tasks.taskhelpersextensions.<casttoobject>d__3`1.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.web.http.controllers.apicontrolleractioninvoker.<invokeactionasynccore>d__0.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.web.http.controllers.actionfilterresult.<executeasync>d__2.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter`1.getresult()\r\n @ system.web.http.dispatcher.httpcontrollerdispatcher.<sendasync>d__1.movenext()", "innerexception": { "message": "an error has occurred.", "exceptionmessage": "the given key not present in dictionary.", "exceptiontype": "system.collections.generic.keynotfoundexception", "stacktrace": " @ system.collections.generic.dictionary`2.get_item(tkey key)\r\n @ system.data.entity.core.mapping.viewgeneration.structures.memberdomainmap.getdomaininternal(memberpath path)\r\n @ system.data.entity.core.mapping.viewgeneration.queryrewriting.fragmentquerykb.createisoftypecondition(memberpath currentpath, ienumerable`1 derivedtypes, memberdomainmap domainmap)\r\n @ system.data.entity.core.mapping.viewgeneration.queryrewriting.fragmentquerykb.createvariableconstraintsrecursion(edmtype edmtype, memberpath currentpath, memberdomainmap domainmap, edmitemcollection edmitemcollection)\r\n @ system.data.entity.core.mapping.viewgeneration.queryrewriting.fragmentquerykb.createvariableconstraintsrecursion(edmtype edmtype, memberpath currentpath, memberdomainmap domainmap, edmitemcollection edmitemcollection)\r\n @ system.data.entity.core.mapping.viewgeneration.viewgencontext..ctor(viewtarget viewtarget, entitysetbase extent, ilist`1 extentcells, cqlidentifiers identifiers, configviewgenerator config, memberdomainmap querydomainmap, memberdomainmap updatedomainmap, entitycontainermapping entitycontainermapping)\r\n @ system.data.entity.core.mapping.viewgeneration.viewgenerator.createviewgencontext(entitysetbase extent, viewtarget viewtarget, cqlidentifiers identifiers)\r\n @ system.data.entity.core.mapping.viewgeneration.viewgenerator.generatedirectionalviewsforextent(viewtarget viewtarget, entitysetbase extent, cqlidentifiers identifiers, keytolistmap`2 views)\r\n @ system.data.entity.core.mapping.viewgeneration.viewgenerator.generatedirectionalviews(viewtarget viewtarget, cqlidentifiers identifiers, keytolistmap`2 views)\r\n @ system.data.entity.core.mapping.viewgeneration.viewgenerator.generateallbidirectionalviews(keytolistmap`2 views, cqlidentifiers identifiers)\r\n @ system.data.entity.core.mapping.viewgeneration.viewgengatekeeper.generateviewsfromcells(list`1 cells, configviewgenerator config, cqlidentifiers identifiers, entitycontainermapping containermapping)\r\n @ system.data.entity.core.mapping.storagemappingitemcollection.viewdictionary.serializedgenerateviews(entitycontainermapping entitycontainermap, dictionary`2 resultdictionary)\r\n @ system.data.entity.core.mapping.storagemappingitemcollection.viewdictionary.serializedgetgeneratedviews(entitycontainer container)\r\n @ system.data.entity.core.common.utils.memoizer`2.result.getvalue()\r\n @ system.data.entity.core.common.utils.memoizer`2.evaluate(targ arg)\r\n @ system.data.entity.core.mapping.storagemappingitemcollection.viewdictionary.getgeneratedview(entitysetbase extent, metadataworkspace workspace, storagemappingitemcollection storagemappingitemcollection)\r\n @ system.data.entity.core.metadata.edm.metadataworkspace.getgeneratedview(entitysetbase extent)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.expandview(scantableop scantableop, isofop& typefilter)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.processscantable(node scantablenode, scantableop scantableop, isofop& typefilter)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.visit(scantableop op, node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitchildren(node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitrelopdefault(relop op, node n)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.visit(filterop op, node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitchildren(node n)\r\n @ system.data.entity.core.query.internaltrees.basicopvisitorofnode.visitdefault(node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitchildren(node n)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.visitscalaropdefault(scalarop op, node n)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.visitnavpropertyop(propertyop op, node n)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.visit(propertyop op, node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitchildren(node n)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.visitscalaropdefault(scalarop op, node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitchildren(node n)\r\n @ system.data.entity.core.query.internaltrees.basicopvisitorofnode.visitdefault(node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitchildren(node n)\r\n @ system.data.entity.core.query.internaltrees.basicopvisitorofnode.visitdefault(node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitchildren(node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitrelopdefault(relop op, node n)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.visit(projectop op, node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitchildren(node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitrelopdefault(relop op, node n)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.visit(projectop op, node n)\r\n @ system.data.entity.core.query.plancompiler.subquerytrackingvisitor.visitchildren(node n)\r\n @ system.data.entity.core.query.internaltrees.basicopvisitorofnode.visitdefault(node n)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.process(dictionary`2& tvfresultkeys)\r\n @ system.data.entity.core.query.plancompiler.preprocessor.process(plancompiler plancompilerstate, structuredtypeinfo& typeinfo, dictionary`2& tvfresultkeys)\r\n @ system.data.entity.core.query.plancompiler.plancompiler.compile(list`1& providercommands, columnmap& resultcolumnmap, int32& columncount, set`1& entitysets)\r\n @ system.data.entity.core.entityclient.internal.entitycommanddefinition..ctor(dbproviderfactory storeproviderfactory, dbcommandtree commandtree, dbinterceptioncontext interceptioncontext, idbdependencyresolver resolver, bridgedatareaderfactory bridgedatareaderfactory, columnmapfactory columnmapfactory)" } }
edit
i started on fresh database , same result entity. leads me believe has dbcontext. looks this:
public class sealingservercontext : dbcontext { public sealingservercontext() : base("name=tfmcontext") { this.database.log = console.write; this.configuration.lazyloadingenabled = false; } public dbset<organization> organizations { get; set; } public dbset<user> users { get; set; } public dbset<role> roles { get; set; } public dbset<reviewerrole> reviewerroles { get; set; } public dbset<annotationtemplate> annotationtemplates { get; set; } public dbset<paperitem> paperitems { get; set; } public dbset<paperraster> paperrasters { get; set; } public dbset<engineerrole> engineerroles { get; set; } public dbset<seal> seals { get; set; } public dbset<signature> signatures { get; set; } public dbset<job> jobs { get; set; } public dbset<jobfile> jobfiles { get; set; } public dbset<pdf> pdfs { get; set; } public dbset<pdfpage> pdfpages { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<annotationtemplate>() .haskey(annotationtemplate => annotationtemplate.id); modelbuilder.entity<jobfile>() .haskey(pdf => pdf.id); modelbuilder.entity<pdf>() .haskey(pdf => pdf.id) .hasmany(pdf => pdf.pdfpages) .withrequired() .hasforeignkey(pdfpage => pdfpage.pdf_id) .willcascadeondelete(true); modelbuilder.entity<job>() .haskey(job => job.id); modelbuilder.entity<job>() .hasmany(job => job.files) .withrequired() .hasforeignkey(jobfile => jobfile.job_id); modelbuilder.entity<job>() .hasmany(job => job.regions) .withrequired() .hasforeignkey(region => region.job_id); modelbuilder.entity<organization>() .haskey(organization => organization.id) .hasmany(org => org.users) .withrequired() .hasforeignkey(user => user.organization_id); modelbuilder.entity<paperitem>() .haskey(paperitem => paperitem.id); modelbuilder.entity<paperraster>() .haskey(paperraster => paperraster.id); modelbuilder.entity<models.files.pdfpage>() .haskey(pdfpage => pdfpage.id); modelbuilder.entity<region>() .haskey(region => region.id); modelbuilder.entity<role>() .haskey(role => role.id); modelbuilder.entity<engineerrole>() .haskey(role => role.id); modelbuilder.entity<engineerrole>() .hasmany(engineerrole => engineerrole.seals) .withrequired() .hasforeignkey(seal => seal.engineerrole_id); modelbuilder.entity<engineerrole>() .hasmany(engineerrole => engineerrole.signatures) .withrequired() .hasforeignkey(signature => signature.engineerrole_id); modelbuilder.entity<engineeradminrole>() .haskey(role => role.id); modelbuilder.entity<revieweradminrole>() .haskey(role => role.id); modelbuilder.entity<reviewerrole>() .haskey(reviewerrole => reviewerrole.id) .hasmany(reviewerrole => reviewerrole.annotationtemplates) .withoptional() .hasforeignkey(annotation => annotation.reviewerrole_id); modelbuilder.entity<seal>() .haskey(seal => seal.id); modelbuilder.entity<signature>() .haskey(signature => signature.id); modelbuilder.entity<user>() .haskey(user => user.id); modelbuilder.entity<user>() .hasmany(user => user.roles) .withoptional() .hasforeignkey(role => role.user_id); } }
edit2
i've been commenting things out , doing db migrations , have narrowed down inheritance hierarchy:
[knowntype(typeof(paperraster))] public class paperitem { public int id { get; set; } public pointf pdfposition { get; set; } public string itemtype { get; set; } public virtual void move(pointf coordinatestranslation) { } protected paperitem(string paperitemtype, pointf pdfposition) { itemtype = paperitemtype; pdfposition = pdfposition; } public virtual paperitem deepcopy() { return new paperitem("paperitem", new pointf()); } public virtual string tojson() { //turn of properties json return ""; } }
and child class:
public class paperraster : paperitem { public string source { get; set; } public string crossorigin { get; set; } public matrix matrix { get; set; } public paperraster(string source, string crossorigin, matrix matrix, pointf pdfposition) : base("raster", pdfposition) { this.source = source; this.crossorigin = crossorigin; this.matrix = matrix; } public paperraster(string json) : base("raster", new pointf()) { jarray array = jarray.parse(json); jtoken type = array[0]; jtoken properties = array[1]; jtoken position = array[2]["pdfposition"]; if (type.tostring() != "raster") { throw new exception("not paperraster"); } var matrix = properties["matrix"]; this.matrix = new matrix(float.parse(matrix[0].tostring()), float.parse(matrix[1].tostring()), float.parse(matrix[2].tostring()), float.parse(matrix[3].tostring()), float.parse(matrix[4].tostring()), float.parse(matrix[5].tostring())); // custom encoded properties // bottom left point of annotation (that's how itextsharp inserts it) var x = position["x"].tostring(); var y = position["y"].tostring(); this.pdfposition = new pointf() { x = float.parse(x), y = float.parse(y) }; } public override void move(pointf coordinatestranslation) { this.matrix.translate(coordinatestranslation.x, coordinatestranslation.y); } public override paperitem deepcopy() { return new paperraster(this.source, this.crossorigin, this.matrix, this.pdfposition); } public override string tojson() { //turn of properties json return ""; } }
if comment out child class , db migration. goes working. in inheritance hierarchy causes ef incorrectly generate tables
other notes
strange sidenote, doing works:
var result = context.dbset.single(x => x.id == 10000);
however using find
so:
var result = context.dbset.find(10000);
causes error above.
i have figure out problem me, related not mapped property inside derived entity (and occurs when entire derived class not mapped). discribed better in this answer.
Comments
Post a Comment