2012-05-22 87 views
4

我正在玩实体框架4和sqlgeography数据类型。我无法让Entity Framework将我的sqlgeography类型映射到db。我已经将我的类定义为POCO,并希望实体框架为我创建表。我没有在这方面取得成功,然后尝试自己创建表,但没有成功。使用实体框架代码优先映射SqlGeography

我的类看起来是这样的:

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> 

回答

0

据我所知,EF4尚不支持的GeographyGeometry数据类型SQL Server 2008.这将是Entity Framework v5的一个功能 - 将在今年晚些时候与.NET 4.5一起发布)。

查看EF5 Spatial Types Walkthrough了解EF5如何支持空间类型。

并参见this blog post by EF goddess Julie Lerman关于如何仍然使用地理类型与EF4 - 有点额外的工作 - 不知道有多少在EF代码首先工作,但是。

另外:see this other SO post on the topic