Mapping SqlGeography using Entity Framework code first

2.9k Views Asked by At

I am playing around with Entity Framework 4 and the sqlgeography datatype. I am having trouble getting Entity Framework map my sqlgeography types to the db. I have defined my class as POCO and would like entity framework to create the tables for me. I didn't succeed in this and have then tried creating the table myself without success either.

My class looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Types;

namespace Nearest.Models
{
    public class Point
    {
        public int Id { get; set; }
        public int DataSet { get; set; }
        public int Author { get; set; }
        public SqlGeography Geo { get; set; }
        public string Data { get; set; }
    }
}

and this is the error I get:

<Exception xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ExceptionType>System.InvalidOperationException</ExceptionType>
<Message>
An error occurred when trying to create a controller of type 'Nearest.Controllers.PointsController'. Make sure that the controller has a parameterless public constructor.
</Message>
<StackTrace>
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpControllerContext controllerContext, Type controllerType) at System.Web.Http.Dispatcher.DefaultHttpControllerFactory.CreateInstance(HttpControllerContext controllerContext, HttpControllerDescriptor controllerDescriptor) at System.Web.Http.Dispatcher.DefaultHttpControllerFactory.CreateController(HttpControllerContext controllerContext, String controllerName) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
</StackTrace>
<InnerException>
<ExceptionType>System.TypeInitializationException</ExceptionType>
<Message>
The type initializer for 'Nearest.Controllers.PointsController' threw an exception.
</Message>
<StackTrace>
at Nearest.Controllers.PointsController..ctor() at lambda_method(Closure ) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpControllerContext controllerContext, Type controllerType)
</StackTrace>
<InnerException>
<ExceptionType>System.Data.DataException</ExceptionType>
<Message>
An exception occurred while initializing the database. See the InnerException for details.
</Message>
<StackTrace>
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source) at Nearest.Models.PointRepository..ctor() in C:\Projects\Nearest\trunk\Nearest\Models\PointRepository.cs:line 15 at Nearest.Controllers.PointsController..cctor() in C:\Projects\Nearest\trunk\Nearest\Controllers\PointsController.cs:line 14
</StackTrace>
<InnerException>
<ExceptionType>System.Data.EntityCommandCompilationException</ExceptionType>
<Message>
An error occurred while preparing the command definition. See the inner exception for details.
</Message>
<StackTrace>
at System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree) at System.Data.EntityClient.EntityProviderServices.CreateCommandDefinition(DbProviderFactory storeProviderFactory, DbCommandTree commandTree) at System.Data.EntityClient.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree) at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Span span, ReadOnlyCollection`1 compiledQueryParameters) at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source) at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence) at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot) at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression) at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source) at System.Data.Entity.Internal.InternalContext.QueryForModelHash() at System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata) at System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass5.<PerformDatabaseInitialization>b__3() at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
</StackTrace>
<InnerException>
<ExceptionType>System.Data.MappingException</ExceptionType>
<Message>
(6,10) : error 3004: Problem in mapping fragments starting at line 6:No mapping specified for properties Point.Geo in Set Points. An Entity with Key (PK) will not round-trip when: Entity is type [Nearest.Models.Point]
</Message>
<StackTrace>
at System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.SerializedGenerateViews(EntityContainer container, Dictionary`2 resultDictionary) at System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.SerializedGetGeneratedViews(EntityContainer container) at System.Data.Common.Utils.Memoizer`2.<>c__DisplayClass2.<Evaluate>b__0() at System.Data.Common.Utils.Memoizer`2.Result.GetValue() at System.Data.Common.Utils.Memoizer`2.Evaluate(TArg arg) at System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.GetGeneratedView(EntitySetBase extent, MetadataWorkspace workspace, StorageMappingItemCollection storageMappingItemCollection) at System.Data.Metadata.Edm.MetadataWorkspace.GetGeneratedView(EntitySetBase extent) at System.Data.Query.PlanCompiler.PreProcessor.ExpandView(Node node, ScanTableOp scanTableOp, IsOfOp& typeFilter) at System.Data.Query.PlanCompiler.PreProcessor.ProcessScanTable(Node scanTableNode, ScanTableOp scanTableOp, IsOfOp& typeFilter) at System.Data.Query.PlanCompiler.PreProcessor.Visit(ScanTableOp op, Node n) at System.Data.Query.InternalTrees.ScanTableOp.Accept[TResultType](BasicOpVisitorOfT`1 v, Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfT`1.VisitNode(Node n) at System.Data.Query.PlanCompiler.SubqueryTrackingVisitor.VisitChildren(Node n) at System.Data.Query.PlanCompiler.SubqueryTrackingVisitor.VisitRelOpDefault(RelOp op, Node n) at System.Data.Query.PlanCompiler.PreProcessor.Visit(ProjectOp op, Node n) at System.Data.Query.InternalTrees.ProjectOp.Accept[TResultType](BasicOpVisitorOfT`1 v, Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfT`1.VisitNode(Node n) at System.Data.Query.PlanCompiler.SubqueryTrackingVisitor.VisitChildren(Node n) at System.Data.Query.PlanCompiler.SubqueryTrackingVisitor.VisitRelOpDefault(RelOp op, Node n) at System.Data.Query.PlanCompiler.PreProcessor.Visit(ProjectOp op, Node n) at System.Data.Query.InternalTrees.ProjectOp.Accept[TResultType](BasicOpVisitorOfT`1 v, Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfT`1.VisitNode(Node n) at System.Data.Query.PlanCompiler.SubqueryTrackingVisitor.VisitChildren(Node n) at System.Data.Query.PlanCompiler.SubqueryTrackingVisitor.VisitRelOpDefault(RelOp op, Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfT`1.VisitSortOp(SortBaseOp op, Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfT`1.Visit(ConstrainedSortOp op, Node n) at System.Data.Query.InternalTrees.ConstrainedSortOp.Accept[TResultType](BasicOpVisitorOfT`1 v, Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfT`1.VisitNode(Node n) at System.Data.Query.PlanCompiler.SubqueryTrackingVisitor.VisitChildren(Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfNode.VisitDefault(Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfNode.VisitPhysicalOpDefault(PhysicalOp op, Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfT`1.Visit(PhysicalProjectOp op, Node n) at System.Data.Query.InternalTrees.PhysicalProjectOp.Accept[TResultType](BasicOpVisitorOfT`1 v, Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfT`1.VisitNode(Node n) at System.Data.Query.PlanCompiler.PreProcessor.Process() at System.Data.Query.PlanCompiler.PreProcessor.Process(PlanCompiler planCompilerState, StructuredTypeInfo& typeInfo) at System.Data.Query.PlanCompiler.PlanCompiler.Compile(List`1& providerCommands, ColumnMap& resultColumnMap, Int32& columnCount, Set`1& entitySets) at System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree)
</StackTrace>
</InnerException>
</InnerException>
</InnerException>
</InnerException>
</Exception>
1

There are 1 best solutions below

0
On

As far as I know, EF4 doesn't yet support the Geography and Geometry datatypes of SQL Server 2008. This will be a feature of Entity Framework v5 -- to be released with .NET 4.5 later this year).

See a EF5 Spatial Types Walkthrough for an explanation of how EF5 will support spatial types.

And see this blog post by EF goddess Julie Lerman on how to still use geography types with EF4 - with a bit of extra work - not sure how much of that works in EF code-first, though.

Also: see this other SO post on the topic