使用Entity Framework Code First映射SqlGeography

4

我正在尝试使用Entity Framework 4和sqlgeography数据类型。我在让Entity Framework将我的sqlgeography类型映射到数据库时遇到了问题。我已经定义了我的类为POCO,希望entity framework可以为我创建表。但我没有成功,然后尝试自己创建表也没有成功。

我的类如下所示:

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; }
    }
}

这是我收到的错误信息:

<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个回答

0
据我所知,EF4尚不支持SQL Server 2008的GeographyGeometry数据类型。这将是Entity Framework v5的一个功能 - 将在今年晚些时候与.NET 4.5一起发布。
请参阅EF5 Spatial Types Walkthrough以了解EF5如何支持空间类型的说明。
并且请参阅EF女神Julie Lerman的此博客文章,了解如何在EF4中仍然使用地理类型 - 需要进行一些额外的工作 - 不确定其中多少适用于EF代码优先。
另外:请参阅此主题的其他SO帖子

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接