2

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!

2
  • Did you do EF-code first or do you have your database and SPs already prior to EF? Commented Sep 14, 2011 at 0:57
  • @Ronald: I created the tables first, and then used "Update Model from Database" by right-clicking on the designer. Commented Sep 14, 2011 at 2:27

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ladislav. Actually, I ended up just adding Latitude and Longitude columns to the table and then adding a trigger to update my geography column, which isn't mapped. Works well;)
2

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.

6 Comments

Thanks, this is definitely good to know. I think I might use import functions as a work around to my problem. Currently, I had (1) opened the designer view for my edmx file, (2) clicked on MyEntity, (3) opened the Mapping Details window, and (4) assigned a seperate stored procedure for each of the CRUD operations (minus the retrieval, making it 3 stored procedures). The problem seems to be occuring because the Latitude and Longitude parameters of my insert stored procedure are not lining up properly with MyEntity, but I'm not sure if it's possible to fix.
@Andrew: That is the correct approach but what you mean that your parameters are not lining up properly with your entity? In short the entity properties must be mapped correctly to stored procedure parameters.
@Ladislav Mrnka: Well, to be a little more accurate, this is a correct approach, but not necessarily the correct approach. I'm not trying to create an import function. I'm trying to redefine the default stored procedures which EF creates on the fly for interacting with the database. As I understand it, there are 3 of these (one for insert, one for update, and one for delete). In Programming Entity Framework by Lerman, she discusses overriding them, the procedure which I tried to use in my previous comment. The problem is that one of the t-sql datatypes has no analogue in EF.
Perhaps the problem is that I need to include these properties (Latitude and Longitude) in the model itself, and then go into the 3 layers of xml and hand code the mapping. I'll try it this evening, as soon as I get back from my other job.
Also, the problem is easy enough to recreate. Just make any table in the sql server 2008 with a geography column. Then try to map an entity to it. That's all I'm trying to do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.