Simone
Simone

Reputation: 2430

How to use spatial type with Entity Framework code-first without referencing Entity Framework

I have an entity with some spatial data... I am using DbGeography and DbGeometry referenced in the System.Data.Entity assembly...

I am not using that one referenced in EntityFramework.dll because I have a layered solution and I do not want to reference Entity Framework everywhere in my solution, but just inside the DAL...

When I try to add a new migration I get the following error:

EntityType 'DbGeometry' has no key defined. Define the key for this EntityType.

So I tried to replace the DbGeometry property with a string OnModelCreating I have tried to write the following code:

modelBuilder.Entity<AddressInfo>()
    .Property(s => s.GeometryLocation)
    .HasColumnType("geometry");

But In this case I receive the error that the String data type is not compatible with the DbGeometry data type...

Does anyone have any idea to solve this problem? Does anyone know which data type is compatible with DbGeometry data type?

Thank you

UPDATE

EntityType 'DbGeography' has no key defined does not solve my problem.. At the end, the author of the post says that

[He] literally had to add a reference to Entity Framework in [his] model

This is exactly what I want to avoid...

Ryan also says that

I think it may be fixable with a clever data configuration map though, I'll be playing with it and will update if I run across any workable techniques

Any idea how to fix it? Which could be a compatible data type to replace DbGeometry?

Upvotes: 5

Views: 1312

Answers (2)

Akos Nagy
Akos Nagy

Reputation: 4350

Somebody might come along and give an answer (and I really hope someone does), but as far as I know, this is just not possible. I have experienced with this myself for quite some time, and I haven't come up with a good, usable solution. You have to add a reference to EntityFramework.dll if you want to use DbGeography, no matter what (don't know about DbGeometry, though).

Here's the documentation:

https://msdn.microsoft.com/en-us/library/system.data.entity.spatial.dbgeography(v=vs.113).aspx

This states that it is in the EntityFramework.dll.

Upvotes: 1

moondaisy
moondaisy

Reputation: 4491

If you are using EF5 you should use DbGeography and DbGeometry referenced inSystem.Data.Spatial.

But if you are using EF6 you should use DbGeography and DbGeometry referenced inSystem.Data.Entity.Spatial.

Upvotes: 2

Related Questions