Use strong spatial types option in model designer locked?
Asked Answered
C

3

7

I'm working on a model first EF6 model in model first approach.

EF exposes more than DBGeometry and DBGeography and allows me to select specific subtypes when designing the model (like GeographyPoint).

However my generated classes are still simply DBGeography even when i select a more specific subtype.

I notice a property on the model that is named "Use Strong Spatial Types" but it is set to false by default and seems locked (greyed out, no dropdown list, can't type). This seems to imply a support for generating stronger typed classes (that would have GeographyPoint instead of DBGeography in the generated class for example)

Any idea what could be causing this? This sounds like a helpful feature.

I'm targeting SQL Server Express 2012, if this is an issue i can switch to another edition as i'm not doing anything versions specific.

UPDATE : see image link if you're not sure you understand what i'm refering to!

https://www.dropbox.com/s/lzgsoi60whuicy0/EF%20Spacial%20Strong.png?dl=0

enter image description here

Charity answered 11/9, 2014 at 23:16 Comment(6)
See one of your current answers starts out "If you are looking for a specific classes like GeographyPoint" implying they are not sure, and the other answerer misunderstood your question. Might get more attention if you edited your question and provided a distinct question: "Any idea what could be causing this? This sounds like a helpful feature." What are the "this"'s referring to? If you make people read between the lines then you are going to get answers that don't address your problem, and a bounty is just going to attract more of those unhelpful answers. Good luck.Tigon
@Tigon I see where you're coming from but when i read and re read my question it seems pretty clear to me that i don't have an "issue or bug" but am asking something very specific. "this" refers to what i mentioned just before, the option being greyed out and no matter how much i re read my post it really seems very clear. Option X is disabled, it's name seems to imply it does Y, Y sounds nice, why can't i enable it.Charity
@Tigon I just edited my post to remove all unneeded detail and try to be even more specific, do you feel it is any better? any tips?Charity
"Why are my generated classes not of the specific subtype, such as GeographyPoint?" is maybe the question you are asking?Tigon
@Tigon Partly, it is expected that they are not since the setting is set to false, so the question is why can't i set it to true (i'm not sure about what the setting does, but making stronger subtypes seems like the only sensible thing i could find out). Which is why i started a Bounty for a more detailed answer, so my question is all of it, 1) can i get stronger subtypes / 2) how / 3) what does this setting do / 4) why is it disabled and how can i enable it. I'm looking for an exhaustive answer on the topic covering everything in my questionCharity
@Tigon I added an image of the setting, it's description and the tooltip that hints it does exactly what i want for clarityCharity
S
6

Written as regards to Entity Framework v6.1.1 implementation.

If you are looking for a specific classes like GeographyPoint instead of DBGeography for your property then there's no such in Entity Framework implementation.

All geography-related stuff for Entity Framework is concentrated in one System.Data.Entity.Spatial.DBGeography class. Instance of such class can act differently based on actual data.

For example, if you work with a Point, you can access Elevation, Longitude and Latitude properties. If it's actually a collection, you can access ElementCount property.

And in your queries you can use DbGeography methods (i.e. Distance()) to perform type-specific queries like:

var myLocation = DbGeography.FromText("POINT(40.7127, 74.0059)");
var results = from p in Points
              orderby p.Distance(myLocation)
              select p;

Actually, I don't see a 'Use strong spatial types' property running VS 2013.3, EF Power Tools Beta 4.

UPDATE

Found it under actual data model properties. Well, it's more of a question to developers of EF, but if you look at the source code of EFEntityModelDescriptor (component that describes all those extension menu items), there are two properties that responsible for Use Strong Spatial Types menu item:

  1. UseStrongSpatialTypes which returns true by-default or calls GetUseStrongSpatialTypesFeatureState:

    internal static FeatureState GetUseStrongSpatialTypesFeatureState(Version schemaVersion)
    {
        Debug.Assert(EntityFrameworkVersion.IsValidVersion(schemaVersion), "Invalid schema version.");
    
        return schemaVersion > EntityFrameworkVersion.Version2
                   ? FeatureState.VisibleAndEnabled
                   : FeatureState.VisibleButDisabled;
    }
    

As you can see, all EF versions above 2 should return FeatureState.VisibleAndEnabled. Current implementation is EntityFrameworkVersion.Version3, but still menu item is disabled and still its value is false. We see false in the designer because of the annotation specified in Conceptual Model in your edmx-file that explicitly set it to false:

    <!-- CSDL content -->
<edmx:ConceptualModels>
  <Schema xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" 
          annotation:UseStrongSpatialTypes="false">
  </Schema>
</edmx:ConceptualModels>

This is set to false on the generation phase in EdmXmlSchemaWriter

        if (_version == XmlConstants.EdmVersionForV3)
        {
            _xmlWriter.WriteAttributeString(
                AnnotationNamespacePrefix,
                XmlConstants.UseStrongSpatialTypes, 
                XmlConstants.AnnotationNamespace,
                XmlConstants.False);
        }

But if EF version is 3 and FeatureState is VisibleAndEnabled why is the menu item is disabled? That leads us to the next property:

  1. IsReadOnlyUseStrongSpatialTypes with the following implementation (true always):

    internal bool IsReadOnlyUseStrongSpatialTypes()
    {
        // TODO: when runtime support for the other (true) setting of this attribute is available replace the "return true" below by the commented line below it
        return true;
        // return (!EdmFeatureManager.GetUseStrongSpatialTypesFeatureState(TypedEFElement.Artifact.SchemaVersion));
    }
    

As you can see, it always returns true and from a TODO comment we can guess that there's no support for this currently. Unfortunately, there's no source code available for older versions of EntityDesign project, so it's hard to track why exactly this was changed.

As Pawel mentioned in this comment, EF team wanted to adopt some existing spatial library (without having to implement and maintain it), but there was no options, so they implemented simpler DbGeography and DbGeometry classes until other options become available.

Probably, since then IsReadOnlyUseStrongSpatialTypes property was changed to always return true, so that Use Strong Spatial Types menu item is greyed-out in the designer and a Description note was added about DbGeography and DbGeometry types used.

SUMMARY

Just to summarize my answer - it's disabled because it is not applicable for Entity Framework v5.0 and onwards (and corresponding versions of Visual Studio) - implementation has changed.

Shut answered 16/9, 2014 at 16:18 Comment(6)
I'm sorry but that's not my question either, i'm having no issue working with those, i'm only asking about the possibility of the setting i see that is disabled, and getting stronger types as generated properties, i already use code similar to yours but this has nothing to do with my question.Charity
Also i do not have the power Tools but i do have VS pro 2013 update 3, when i'm on a model and click on the model i do have "use strong spacial types" set to false and disabled in the model properties, are you sure you don't have that?Charity
I added an image at the end of my original post to show you what setting i'm talking about as well as the tooltip indicating it does what i think it doesCharity
exactly the detailed answer i was hoping for, thanks!Charity
Nice write-up however I don't think the paragraph starting with "However to me this looks..." is accurate. The EF designer works mostly on csdl/msl/ssdl artifacts (or to be more accurate on an objectified version of the artifacts) and the ObjectContext vs. DbContext is just about using a code generator that spits out ObjectContext or DbContext based code. See my response below for some historical context to the story.Cantina
Thanks Pawel for clarifying this, I've added a reference to your comment instead of that paragraph.Shut
C
2

The reason why the "Use Strong Spatial Types" option is grayed out in the EF designer is that false is the only option allowed by the EF runtime. Now, obviously the question is - why is this the only value supported by the runtime. You can find the answer in the code - if you look at the TypeUsageBuilder class you will find the following comment around line 200:

// Forward compat FUTURE SYSTEM.SPATIAL
// for now we treat all Geographic types the same, and likewise for geometry.
// to allow us to later introduce the full heirarchy without breaking back compat
// we require spatial types to have the IsStrict facet with a false value.    
// Set this facet to false if the schema has the UseStrongSpatialTypes attribute with the a false.

From what I remember as a member of the EF team at that time we (as the EF team) did not want to be in the business of creating and maintaining our own spatial library as this is not what an ORM team should focus on. There wasn't also any spatial library at that time we could adopt so we decided to go with something simple that would work (read the DbGeometry and DbGeography types). I think the hope was that we would be able to use this spatial library which was being started developed at that time but it never happened. Also note that the EDM type system (used in CSDL) contains the notion of spatial subtypes (ln. 183) but when they need to be translated to a Clr type the DbGeometry or DbGeography type will be returned (see: PrimitiveType ln. 181)

Cantina answered 17/9, 2014 at 6:13 Comment(0)
U
0

You need to download and install Microsoft® System CLR Types for Microsoft® SQL Server® 2012 SP1 form the Microsoft SQL Server 2012 Feature Packs site (or if without SP1: MS SQL Server 2012 Feature Pack).

Prerequisites for spatial types with Microsoft SQL Server

SQL Server spatial support depends on the low-level, SQL Server-specific types SqlGeography and SqlGeometry. These types live in Microsoft.SqlServer.Types.dll assembly, and this assembly is not shipped as part of EF or as part of the .NET Framework.

When Visual Studio is installed it will often also install a version of SQL Server, and this will include installation of the Microsoft.SqlServer.Types.dll.

If SQL Server is not installed on the machine where you want to use spatial types, or if spatial types were excluded from the SQL Server installation, then you will need to install them manually. The types are included in the SQL Server Feature Packs, and different assemblies exist for SQL Server 2008 and SQL Server 2012:

Filename: Filename: X86 and x64 Package(SQLSysClrTypes.msi)

The SQL Server System CLR Types package contains the components implementing the geometry, geography, and hierarchy id types in SQL Server 2012. This component can be installed separately from the server to allow client applications to use these types outside of the server.

Unpaged answered 14/9, 2014 at 17:42 Comment(4)
Are you sure you understood the question? Spacial types are working just fine for me that's not the problem at all. My question relates to how entity Framework générâtes C# code for them nothing else.Charity
Just to be clear, EF generated DBGeography just fine and queries it just fine too, and sql server isn't complaining, my issue is only about activating a stronger type feature of EF for which i can't find any documentation. I can see things like this in my query : (([Project1].[StartLocation].STDistance(@p__linq__3)) <= @p__linq__5). SQL server works fine, EF works fine, spacial types are already working fine so i don't think the pack would help at all going from the descriptionCharity
@RonanThibaudau: I interpreted this statement as being relevant to your issue: "This component can be installed separately from the server to allow client applications to use these types outside of the server." As I understand your question, you are having difficulties using these types outside the server.Unpaged
No i'm not having any dificulties using these types outside the server at all, you missunderstood the question, my question has nothing to do with those types to begin with (DBGeometry and DBGeography) but with strongly typed subtypes exposed in entity Framework and an option (disabled, and why is my question) that allows you to use more fine grained types than those. Entity Framework allows you to specify that something will be a point for example, but still generates a standard DBGeometry in code, and has a setting that hints at something better but is disabled.Charity

© 2022 - 2024 — McMap. All rights reserved.