Andrew
Andrew

Reputation: 6514

Entity Framework: Difficulty Mapping Function Parameters

I'm trying to create an entity in my model that has two properties for specifying it's latitude and longitude. In Sql Server 2008 R2, I'm using a single column in a table to store this information. The column is of type geography. In order to use the entity against the database, I was hoping to map 3 stored procedures to insert, update, and delete rows from the appropriate table in the database. However, I'm having trouble getting the assembly containing my model to compile without any errors. I'm still pretty new to using EF 4, so I'm not quite sure how to do this correctly. My partial class looks like:

public partial class MyEntity
{
    private double latitude;
    private double longitude;

    public double Latitude { get {return latitude;} set {latitude = value; }
    public double Longitude { get {return longitude;} set {longitude = value; }
}

My stored procedures take parameters of Latitude and Longitude (except for the delete stored procedure). However, when I try to compile the project, I get the error:

A mapping function bindings specifies a function MyNamespace.Store.sp_insertMyEntity but does not map the following function parameters: Latitude, Longitude.

I must be declaring the properties incorrectly, somehow. Perhaps it's in my model. Any suggestions will be welcome!

Upvotes: 2

Views: 4148

Answers (2)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364369

As I understand your current entity doesn't map Latitude and Longitude. That means you can never load them from database and you also cannot save them to database - no way.

Your stored procedure cannot expect any parameter which is not supported by EF. If your database table uses Geography data type you must also provide view which will break it into separate Latitude and Longitude columns. Your entity must have Longitude and Latitude as properties and must be mapped to the view. Your procedures must accept mapped Latitude and Longitude as parameters.

Next major version of EF should support Geography directly.

Upvotes: 3

Ronald
Ronald

Reputation: 1542

Assuming that you have used ADO.NET Entity Data Model to map between EF and your database the easiest way to map to a SP is to used the wizard in the Model Browser.

  1. Open Model Browser
  2. Select the Store node
  3. Go to Stored Procedures and select the stored procedure you would like to map with
  4. Right click on the item and select Function Import. The wizard dialog will appear.
  5. Click the button Get Column Information
  6. Click the button Create Complex Type, this will create a type based on the column information
  7. In the radio controls, select Complex and associate the new complex type that was created
  8. Click OK

This will create a new function that you can use. It will handle communicating with your SP including the parameters while you can use it just like an ordinary function.

Upvotes: 2

Related Questions