6 August 2009 1 Comment

Tip 30 – How to use a custom database function

Imagine you have a database function like the DistanceBetween function in Nerd Dinner : CREATE FUNCTION [dbo].[DistanceBetween](    @Lat1 as real ,    @Long1 as real ,    @Lat2 as real ,    @Long2 as real ) RETURNS real AS BEGIN … END And you want to use it with the Entity Framework. Declaring the Function The first step is to open your EDMX file in the XML editor and add a <Function> inside the <Schema> element of the <edmx:StorageModels> element. When you are done the function declaration (akin to a C++ header file or an extern declaration) should look like this: <Function Name=”DistanceBetween”           IsComposable=”true”            Schema=”dbo”            Aggregate=”false”           BuiltIn=”false”           ReturnType=”float”>    <Parameter Name=”Lat1″ Type=”float” Mode=”In”/>    <Parameter Name=”Long1″ Type=”float” Mode=”In”/>    <Parameter Name=”Lat2″ Type=”float” Mode=”In”/>    <Parameter Name=”Long2″ Type=”float” Mode=”In”/> </Function> Using the Function in eSQL Now you can call this function in eSQL like this: SELECT VALUE(D) FROM MyModel .Dinners AS D WHERE StorageNamespace .DistanceBetween(       D.Latitude,D.Longitude,-34,174) < 50 MyModel is simply the name of your EntityContainer (generally the same as your ObjectContext) and StorageNamespace is your storage model schema namespace.

View post:
Tip 30 – How to use a custom database function

If you liked this post, buy me a Coffee.

One Response to “Tip 30 – How to use a custom database function”

  1. Pett 8 August 2009 at 3:06 pm #

    Greatings, Everything dynamic and very positively! :)


Leave a Reply