实体框架v5发布候选者在模型中使用枚举时抛出异常

时间:2012-05-16 15:45:21

标签: c# entity-framework exception entity-framework-5

我正在使用最新的EF v5候选版本per announcement here。 我的模型是通过EntityTypeConfiguration ...

使用映射文件

当我尝试用枚举(drop + recreate db)替换模型上的字符串属性时,我遇到了这个异常:

System.NotSupportedException was unhandled by user code
  Message=The enum or spatial property 'Category' on type 'Vehicle' cannot be mapped. Use DbModelBuilderVersion 'V5_0' or later to map enum or spatial properties.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.ModelConfiguration.Mappers.PropertyFilter.ValidatePropertiesForModelVersion(Type type, IEnumerable`1 explicitlyMappedProperties)
       at System.Data.Entity.ModelConfiguration.Mappers.PropertyFilter.GetProperties(Type type, Boolean declaredOnly, IEnumerable`1 explicitlyMappedProperties, IEnumerable`1 knownTypes)
       at System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapStructuralElements[TStructuralTypeConfiguration](Type type, ICollection`1 annotations, Action`2 propertyMappingAction, Boolean mapDeclaredPropertiesOnly, Func`1 structuralTypeConfiguration)
       at System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(Type type)
       at System.Data.Entity.DbModelBuilder.<>c__DisplayClass7.<MapTypes>b__1(Type type)
       at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
       at System.Data.Entity.ModelConfiguration.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
       at System.Data.Entity.DbModelBuilder.MapTypes(EdmModel model)
       at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
       at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
       at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
       at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       at System.Data.Entity.Internal.InternalContext.Initialize()
       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.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at WebApp.Controllers.VehiclesController.Get() in C:\WebApp\Controllers\VehiclesController.cs:line 35
       at lambda_method(Closure , Object , Object[] )
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.Execute(HttpControllerContext controllerContext, IDictionary`2 arguments)
       at System.Web.Http.Controllers.ApiControllerActionInvoker.<>c__DisplayClass2.<InvokeActionAsync>b__0()
       at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
  InnerException: 

然后我尝试用[DbModelBuilderVersion(DbModelBuilderVersion.Latest)]装饰我的MyAppDbContext并得到同样的错误...... Finnaly我尝试了[DbModelBuilderVersion(DbModelBuilderVersion.V5_0)],我又得到了另一个例外:

System.Data.MetadataException was unhandled by user code
  Message=Schema specified is not valid. Errors: 
(2,9) : warning 0005: Could not find schema information for the attribute 'Namespace'.
(2,41) : warning 0005: Could not find schema information for the attribute 'Alias'.
(2,54) : error 0005: The 'http://schemas.microsoft.com/ado/2009/02/edm/annotation:UseStrongSpatialTypes' attribute is not declared.
(2,2) : error 0010: The element Schema in namespace http://schemas.microsoft.com/ado/2009/11/edm was unexpected for the root element. The expected Schema in one of the following namespaces: http://schemas.microsoft.com/ado/2006/04/edm, http://schemas.microsoft.com/ado/2007/05/edm, http://schemas.microsoft.com/ado/2008/09/edm.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Metadata.Edm.EdmItemCollection.LoadItems(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, SchemaDataModelOption dataModelOption, DbProviderManifest providerManifest, ItemCollection itemCollection, Boolean throwOnError)
       at System.Data.Metadata.Edm.EdmItemCollection.Init(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths, Boolean throwOnError)
       at System.Data.Metadata.Edm.EdmItemCollection..ctor(IEnumerable`1 xmlReaders)
       at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ToEdmItemCollection(EdmModel model)
       at System.Data.Entity.ModelConfiguration.Edm.Db.Mapping.DbDatabaseMappingExtensions.ToMetadataWorkspace(DbDatabaseMapping databaseMapping)
       at System.Data.Entity.Internal.CodeFirstCachedMetadataWorkspace..ctor(DbDatabaseMapping databaseMapping)
       at System.Data.Entity.Infrastructure.DbCompiledModel..ctor(DbModel model)
       at System.Data.Entity.Infrastructure.DbModel.Compile()
       at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
       at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       at System.Data.Entity.Internal.InternalContext.Initialize()
       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.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at WebApp.Controllers.VehiclesController.Get() in C:\WebApp\Controllers\VehiclesController.cs:line 35
       at lambda_method(Closure , Object , Object[] )
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.Execute(HttpControllerContext controllerContext, IDictionary`2 arguments)
       at System.Web.Http.Controllers.ApiControllerActionInvoker.<>c__DisplayClass2.<InvokeActionAsync>b__0()
       at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
  InnerException:

我的控制器是ApiController,断线如下:

var vehicles = _db.Vehicles.ToList();

BTW,我在VS 2010(w / .NET 4)上使用ASP.NET MVC 4 beta + Web API。

有什么想法吗?

1 个答案:

答案 0 :(得分:8)

枚举和所有其他新的EF 5.0功能都依赖于.NET 4.5(VS11) - 两者目前都处于测试阶段。如果在VS 2010中运行EF 5.0,则会获得与EF 4.3.1 = no enums相同的功能。