c# - Subclassing DataTable -
i'm using entity framework , stored proc takes table valued parameter (tvp). this, need create datatable , populate so:
var = new datatable(); i.columns.add("type", typeof(byte)); i.columns.add("code", typeof(int)); i.rows.add(0, 0); i.rows.add(2, 31); i.rows.add(4, 3121); i.rows.add(4, 3111); and can pass parameter call sqlquery
var result = ctx.database.sqlquery<myresult>("select * mystoredproc(@i)", new sqlparameter("@i",i) { typename = "mytvp" }); now simplify this, recreating columns every time, though subclass datatable, this:
public class mytvp : datatable { public mytvp() : base() { columns.add("type", typeof(byte)); columns.add("code", typeof(int)); } } so code create , populate table this:
var = new mytvp(); i.rows.add(0, 0); i.rows.add(2, 31); i.rows.add(4, 3121); i.rows.add(4, 3111); but if try pass parameter stored proc argumentexception:
no mapping exists object type mytvp known managed provider native type. is there way subclass datatable such can pass stored proc?
i worked around problem having mytvp wrap datatable instead , have property exposes datatable, it's little messy.
stacktrace:
@ system.data.sqlclient.metatype.getmetatypefromvalue(type datatype, object value, boolean inferlen, boolean streamallowed) @ system.data.sqlclient.sqlparameter.getmetatypeonly() @ system.data.sqlclient.sqlparameter.get_dbtype() @ system.data.entity.infrastructure.interception.databaselogformatter.logparameter[tresult](dbcommand command, dbcommandinterceptioncontext`1 interceptioncontext, dbparameter parameter) @ system.data.entity.infrastructure.interception.databaselogformatter.logcommand[tresult](dbcommand command, dbcommandinterceptioncontext`1 interceptioncontext) @ system.data.entity.infrastructure.interception.databaselogformatter.executing[tresult](dbcommand command, dbcommandinterceptioncontext`1 interceptioncontext) @ system.data.entity.infrastructure.interception.databaselogformatter.readerexecuting(dbcommand command, dbcommandinterceptioncontext`1 interceptioncontext) @ system.data.entity.infrastructure.interception.dbcommanddispatcher.<reader>b__d(idbcommandinterceptor i, dbcommand t, dbcommandinterceptioncontext`1 c) @ system.data.entity.infrastructure.interception.internaldispatcher`1.dispatch[ttarget,tinterceptioncontext,tresult](ttarget target, func`3 operation, tinterceptioncontext interceptioncontext, action`3 executing, action`3 executed) @ system.data.entity.infrastructure.interception.dbcommanddispatcher.reader(dbcommand command, dbcommandinterceptioncontext interceptioncontext) @ system.data.entity.internal.interceptabledbcommand.executedbdatareader(commandbehavior behavior) @ system.data.common.dbcommand.executereader(commandbehavior behavior) @ system.data.entity.core.objects.objectcontext.executestorequeryinternal[telement](string commandtext, string entitysetname, executionoptions executionoptions, object[] parameters) @ system.data.entity.core.objects.objectcontext.<>c__displayclass65`1.<executestorequeryreliably>b__64() @ system.data.entity.core.objects.objectcontext.executeintransaction[t](func`1 func, idbexecutionstrategy executionstrategy, boolean startlocaltransaction, boolean releaseconnectiononsuccess) @ system.data.entity.core.objects.objectcontext.<>c__displayclass65`1.<executestorequeryreliably>b__63() @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.execute[tresult](func`1 operation) @ system.data.entity.core.objects.objectcontext.executestorequeryreliably[telement](string commandtext, string entitysetname, executionoptions executionoptions, object[] parameters) @ system.data.entity.core.objects.objectcontext.executestorequery[telement](string commandtext, executionoptions executionoptions, object[] parameters) @ system.data.entity.internal.internalcontext.<>c__displayclass14`1.<executesqlquery>b__13() @ system.data.entity.internal.lazyenumerator`1.movenext() @ system.collections.generic.list`1..ctor(ienumerable`1 collection) @ system.linq.enumerable.tolist[tsource](ienumerable`1 source) @ consoleapplication2.program.main(string[] args) in c:\users\matt.burland\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\program.cs:line 72 @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args) @ system.appdomain.executeassembly(string assemblyfile, evidence assemblysecurity, string[] args) @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly() @ system.threading.threadhelper.threadstart_context(object state) @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.threadhelper.threadstart()
try changing parameter to:
new sqlparameter("@i", i) { typename = "mytvp", sqldbtype = sqldbtype.structured }
Comments
Post a Comment