Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geography field mapping add 'TempId1' to column name #3034

Closed
unairoldan opened this issue Sep 5, 2015 · 10 comments
Closed

Geography field mapping add 'TempId1' to column name #3034

unairoldan opened this issue Sep 5, 2015 · 10 comments

Comments

@unairoldan
Copy link

I am creating an API usign Asp.Net 5 (beta 6), with Entity Framework 7.0.0-beta6.

This is the project.json file:

{
  "dependencies": {
    "System.Collections": "4.0.10-beta-*",
    "System.Linq": "4.0.0-beta-*",
    "System.Threading": "4.0.10-beta-*",
    "Microsoft.CSharp": "4.0.0-beta-*",
    "Microsoft.AspNet.Mvc": "6.0.0-beta6-*",
    "EntityFramework.SqlServer": "7.0.0-beta6",
    "Microsoft.Spatial": "6.13.0"   // I have also tried with System.Spatial
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  }
}

And here is my model and EF entity:

    public class Address
    {
        public int CompanyId { get; set; }
        public string CountryId { get; set; }
        public string AreaId { get; set; }
        public string City { get; set; }
        public decimal Latitude { get; set; }
        public decimal Longitude { get; set; }
        public Geography Location { get; set; }
        public DateTime UpdatedAt { get; set; }
    }

CREATE TABLE [Company].[Address]
(
    ...other fields

    [Location] GEOGRAPHY NOT NULL,

    ...other fields
)
GO

All the fields in my entity Address (and in every entity in my project) is working and is correctly mapped, but in case of Geography field, in this case called "Location", I get this error:

System.Data.SqlClient.SqlException
Invalid column name 'LocationTempId1'.
at System.Data.SqlClient.SqlCommand.<>c__DisplayClass16.<ExecuteDbDataReaderAsync>b__17(Task<SqlDataReader> result) 
at System.Threading.Tasks.ContinuationResultTaskFromResultTask<TAntecedentResult, TResult>.InnerInvoke() 
at System.Threading.Tasks.Task.Execute() 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter<TResult>.GetResult() 
at Microsoft.Data.Entity.Query.AsyncQueryingEnumerable.AsyncEnumerator.<MoveNext>d__5.MoveNext()

EF is trying to get the column LocationTempId1, I do not the reason.

I have tried to solve adding the explicit column mapping in my model definition, but it does not work:

entity.Property(a => a.Location).HasColumnName("Location");

Does anyone know how to solve this issue?

@ErikEJ
Copy link
Contributor

ErikEJ commented Sep 5, 2015

Not possible yet, it is in progess for after RTM #1100

@rowanmiller
Copy link
Contributor

As @ErikEJ mentioned, geography types are not yet supported

@rowanmiller
Copy link
Contributor

@smitpatel can you check what we do in the latest code base. We should throw in this case but the error about LocationTempId1 seems incorrect

@rowanmiller rowanmiller added this to the Investigation milestone Sep 8, 2015
@rowanmiller
Copy link
Contributor

@smitpatel can you also follow up with us about the exception we do throw in this case so that we can verify it is something we are happy with

@smitpatel
Copy link
Contributor

Adding Geography type throws following exception while adding migration since we are pulling the type as entity type.

dnx : Microsoft.Data.Entity.Metadata.ModelItemNotFoundException: The entity type 'Microsoft.Spatial.CoordinateSystem' requires a key to be defined.
At line:1 char:1
+ dnx ef migrations add Test

I am still not able to figure out how the error about LocationTempId1 arise. @unairoldan - Can you share the code you are trying to run.

@unairoldan
Copy link
Author

I don't use EF migrations to generate my entities; this error is thrown when I execute my API trying to get some entity via REST: http://localhost:61241/api/company

In any case, if you still needing the code, I can share the code in a repository.

@smitpatel
Copy link
Contributor

@unairoldan - with the information - "migrations are not used", I am able to get a repro on this. It looks like that model validation rules not being run properly.
The exact cause of LocationTempId - Geography is being discovered as entityType here. Since Geography does not have PK defined, while creating relationship between Address and Geography, EF will add temporary referenced key TempId, which should be replaced by user later or it would throw on validation (this is not happening atm). The FK property name for nav property Location where referenced key is TempId becomes LocationTempId which does not exist in database throwing this exception.

@smitpatel
Copy link
Contributor

Root cause: ModelValidator is not checking if entityType has PK defined or not. This throws exception in ModelDiffer for migration while accessing the PK. The runtime model passes validation and doesn't throw error. I have filed #3109 for that.

As for the original issue here, since spatial data types are not supported they would be expanded assuming entity types. Therefore model you get is a lot different from the database tables and query fails.

@rowanmiller - While adding non-supported type as navigation, we may encounter entity type without PK which would throw in validation or we may get PK set by convention which would be valid model and no error will be thrown. Is this behavior fine?

@rowanmiller
Copy link
Contributor

@smitpatel yes, that is what we would expect to see

@smitpatel
Copy link
Contributor

Once #3109 is resolved, user will get exception about null PK. That should block usage of model far earlier than reaching the exception about wrong column name. Closing this out as there is nothing actionable here.

Better exception while spatial/complex type expanded as entitytype -> #3109
Spatial types -> #1100

@smitpatel smitpatel removed their assignment Jan 12, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants