5

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?

Community
  • 1
  • 1
Simone
  • 2,304
  • 6
  • 30
  • 79
  • 1
    Possible duplicate of [EntityType 'DbGeography' has no key defined](http://stackoverflow.com/questions/19551142/entitytype-dbgeography-has-no-key-defined) – moondaisy Mar 22 '17 at 20:09
  • When you reference the DAL then you have to reference EF as well if you want to use the DAL – Sir Rufo Mar 22 '17 at 20:13
  • @Sir Rufo: Everything is correctly encapsulated inside the DAL and with some other patterns I untied every other layer from EF – Simone Mar 22 '17 at 20:31
  • 1
    @moondaisy: I had already read that but it not solve my problem.. Infact Ryan says that he referenced EF in his model... But he says also 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"... I am looking for that solution – Simone Mar 22 '17 at 20:37
  • If you layer out your BLL also with business objects to then consume in the App layer, you can translate your Entities to those objects, hence only your BLL needs to know about EF. Problem arises when you pass your entities all the way up to the Application layer, instead... I'm in that trouble .... okok 1 year old post, I know – Shockwaver Jun 12 '19 at 09:08

2 Answers2

2

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.

moondaisy
  • 4,303
  • 6
  • 41
  • 70
  • 2
    `System.Data.Entity.Spatial` namespace is inside `EntityFramework.dll`... If I use that, every layer would depende on Entity Framework – Simone Mar 22 '17 at 20:28
1

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.

Akos Nagy
  • 4,201
  • 1
  • 20
  • 37