2012-05-22 3 views
4

저는 Entity Framework 4와 sqlgeography 데이터 타입을 가지고 놀고 있습니다. Entity Framework에서 내 sqlgeography 유형을 db에 매핑하는 데 문제가 있습니다. 내 수업을 POCO로 정의했으며 엔티티 프레임 워크가 나를위한 표를 만들길 원합니다. 나는이 일에 성공하지 못했고 그 다음 테이블을 성공없이 만들려고 시도했다.Entity Framework 코드를 사용하여 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; } 
    } 
} 

을 그리고 이것은 내가 오류입니다 : EF4 아직의 GeographyGeometry 데이터 유형을 지원하지 않는 내가 아는 한

<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

SQL Server 2008. 이것은 Entity Framework v5의 기능으로 올해 말에 .NET 4.5와 함께 출시 될 예정입니다.

EF5가 공간 유형을 지원하는 방법에 대한 설명은 EF5 Spatial Types Walkthrough을 참조하십시오.

그리고 EF4에서 지리 유형을 사용하는 방법에 대해 this blog post by EF goddess Julie Lerman을 참조하십시오. 추가 작업이 약간 필요하지만 EF 코드에서 처음 작동하는 항목의 양은 확실하지 않습니다.

see this other SO post on the topic