1

我一直在使用 EF6 在 VS2013 中开发 MVC5 应用程序。我在开发环境中没有遇到任何问题,但是在发布到 Win 2012 IIS 服务器并运行应用程序时,我遇到了许多页面错误。

我面临的错误似乎在所有页面中都是一致的。

“属性‘CountFrequency’不是字符串或字节数组。只能为字符串和字节数组属性配置长度。”

第 35 行:第 36 行:第 37 行:@foreach(模型中的变量项) 第 38 行:{ 第 39 行:

这是堆栈跟踪:

*

[InvalidOperationException: The property 'CountFrequency' is not a String or Byte array. Length can only be configured for String and Byte array properties.]
   System.Data.Entity.ModelConfiguration.Configuration.ConventionPrimitivePropertyConfiguration.HasMaxLength(Int32 maxLength) +612
   System.Data.Entity.ModelConfiguration.Conventions.PrimitivePropertyAttributeConfigurationConvention`1.<.ctor>b__1(ConventionPrimitivePropertyConfiguration configuration, IEnumerable`1 attributes) +114
   System.Data.Entity.ModelConfiguration.Configuration.PropertyConfigurationConventionDispatcher.Dispatch() +38
   System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyPropertyConfiguration(PropertyInfo propertyInfo, Func`1 propertyConfiguration, ModelConfiguration modelConfiguration) +176
   System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyPropertyConfiguration(PropertyInfo propertyInfo, Func`1 propertyConfiguration, ModelConfiguration modelConfiguration) +218
   System.Data.Entity.ModelConfiguration.Mappers.PropertyMapper.MapPrimitiveOrComplexOrEnumProperty(PropertyInfo propertyInfo, Func`1 structuralTypeConfiguration, Boolean discoverComplexTypes) +650
   System.Data.Entity.ModelConfiguration.Mappers.<>c__DisplayClass14.<MapEntityType>b__e(PropertyMapper m, PropertyInfo p) +46
   System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapStructuralElements(Type type, ICollection`1 annotations, Action`2 propertyMappingAction, Boolean mapDeclaredPropertiesOnly, Func`1 structuralTypeConfiguration) +516
   System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(Type type) +876
   System.Data.Entity.<>c__DisplayClassd.<MapTypes>b__7(Type type) +16
   System.Linq.WhereListIterator`1.MoveNext() +165
   System.Data.Entity.Utilities.IEnumerableExtensions.Each(IEnumerable`1 ts, Action`1 action) +168
   System.Data.Entity.DbModelBuilder.MapTypes(EdmModel model) +428
   System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) +281
   System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +288
   System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +94
   System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +248
   System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +618
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +26
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +72
   System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +21
   ASP._Page_Views_Logic_List_cshtml.Execute() in c:\inetpub\Internal\KSCycleCounts\Views\Logic\List.cshtml:37
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +143
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +110
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +380
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +109
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +890
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +97
   System.Web.Mvc.Async.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult) +241
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +51
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

*

我知道我的连接字符串是有效的并且正在工作。在 Windows 2012 中,我安装了所有角色(ASP.NET 4.5 和 .NET 4.5)。在 IIS 中,我已将我的应用程序池配置为使用 .NET 4,并使用管理员帐户作为测试身份。

谢谢!

4

2 回答 2

3

Though the question is almost a year old at the time of my answer, I recently encountered the same exception when trying to loop through an Entity Framework DbSet.

I initially had a string as a field:

public class Table
{
  [StringLength(20)]
  public string Ref {get;set;}
}

Which I later changed to an int:

public class Table
{
  [StringLength(20)]  // <-- hint: int doesn't need a string-length limitation
  public int Ref {get;set;}
}

Notice that I kept the StringLength attribute... but it naturally has no bearing on an int, and ended up throwing the exception. Silly me. Hope it saves someone else from the hours I lost and shall never regain :)

于 2014-09-06T05:33:05.497 回答
0

我也有同样的问题。您正在访问的表并不总是导致错误的表。对我来说,我正在访问一个叫做bnapp_tr做类似的表

List<bnapp_tr> bn = db.bnapp_tr.ToList();

但是错误出现在我后来创建的一个类中,该类有一个错误注释的字段,即

[StringLength(25)] // <---Here
public decimal? frec_no { get; set; }

StringLength注释不适用于decimal?. 它没有任何意义。但是我在编码时不小心这样做了。所以我只是删除了注释。

于 2017-06-27T21:33:07.830 回答