c# - How to get Entity Framework 6 to use SQL STUFF function inside CSDL? -



c# - How to get Entity Framework 6 to use SQL STUFF function inside CSDL? -

i using ef 6.1 , create custom function in csdl section of edmx file can phone call stuff function built sql 2012. have simple. (note: assumes time hhmm without colon)

<function name="stringtodate" returntype="datetime"> <parameter name="strdate" type="string" /> <parameter name="strtime" type="string" /> <definingexpression> cast(case when strdate &lt;&gt; '' strdate + ' ' + stuff(strtime, 3, 0, ':') end datetime) </definingexpression> </function>

the above code works if remove "stuff" command "stuff" command "'stuff' cannot resolved valid type or function."

i can utilize "entity.sqlserver.sqlfunctions.stuff" in linq entity fine not in csdl.

note: using stuff command insert colon between 2nd , 3rd character in time variable.

edit: "work around" here work around still know how utilize stuff in csdl if possible.

cast(case when strdate &lt;&gt; '' strdate + ' ' + substring(strtime, 1, 2) + ':' + substring(strtime, 3, 2) end datetime)

edit: have posted issue on codeplex. please vote if interested. https://entityframework.codeplex.com/workitem/2583

functions declared in database provider available under provider's namespace. if normal entity sql query should able invoke stuff() adding sqlserver before it, e.g. sqlserver.stuff(strtime, 3, 0, ':').

however, entity sql in body of model-defined functions can refer other canonical functions or other model-defined functions, i.e. model-defined functions cannot made provider specific referring provider specific functions.

this limitation adopted choice. when application executes linq query can execute provider specific functions because application depends on total model conceptual , store schemas , mapping specification. model-defined functions on other hand part of conceptual model, supposed self-contained: should able swap provider, store schema or mapping specification different ones without ever rendering conceptual model invalid. hence things defined in conceptual model cannot depend on things defined elsewhere.

workaround:

i did experimentation , came naïve way simulate sql server's stuff() using canonical functions should work across providers. can utilize own model-defined function specifying namespace of conceptual model, e.g. model1.stuff(strtime, 3, 0, ':')

<function name="stuff" returntype="string"> <parameter name="character_expression" type="string" /> <parameter name="start" type="int32" /> <parameter name="length" type="int32" /> <parameter name="replacewith_expression" type="string" /> <definingexpression> left(character_expression,start-1) + replacewith_expression + substring(character_expression, start + length, length(character_expression)) </definingexpression> </function>

c# linq entity-framework

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -