我尝试切换列值。我正在使用Entity框架数据库连接。
public static void yayinDegistir(int id)
{
using (vt dc = new vt())
{
Testler d = dc.testler.FirstOrDefault(t => t.testId.Equals(id));
if (d != null)
{
d.yayinda = !d.yayinda; //bool value to toggle
dc.SaveChanges(); //error
}
}
}
错误是
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The type 'System.Int32' needs to represent an enumeration type.
Testler类
public class Testler
{
[Key]
[ScaffoldColumn(false)]
public virtual int testId { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Test Adı")]
public virtual string testAdi { get; set; }
[DataType(DataType.MultilineText)]
public virtual string aciklama { get; set; }
[DataType(DataType.Text)]
[EnumDataType(typeof(int), ErrorMessage = "{0} mutlaka sayı olmalıdır!")]
public virtual int testSuresi { get; set; }
public virtual bool cokluSecim { get; set; }
[DataType(DataType.MultilineText)]
public virtual string oneriler { get; set; }
public virtual bool yayinda { get; set; }
public virtual ICollection<Sorular> sorular { get; set; }
}
堆栈错误跟踪
堆栈追踪:
[InvalidOperationException: The type 'System.Int32' needs to represent an enumeration type.] System.ComponentModel.DataAnnotations.EnumDataTypeAttribute.IsValid(Object
值)+40216 System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(对象 value,ValidationContext validationContext)+115 System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(对象 value,ValidationContext validationContext)+29 System.Data.Entity.Internal.Validation.ValidationAttributeValidator.Validate(EntityValidationContext entityValidationContext,InternalMemberEntry属性)+100
[DbUnexpectedValidationException: An unexpected exception was thrown during validation of 'Test Süresi (saniye) (0 süresiz)' when
调用 System.ComponentModel.DataAnnotations.EnumDataTypeAttribute.IsValid。 有关详细信息,请参阅内部异常。] System.Data.Entity.Internal.Validation.ValidationAttributeValidator.Validate(EntityValidationContext entityValidationContext,InternalMemberEntry属性)+170 System.Data.Entity.Internal.Validation.PropertyValidator.Validate(EntityValidationContext entityValidationContext,InternalMemberEntry属性)+108 System.Data.Entity.Internal.Validation.EntityValidator.ValidateProperties(EntityValidationContext entityValidationContext,InternalPropertyEntry parentProperty,List
1 validationErrors) +100 System.Data.Entity.Internal.Validation.TypeValidator.Validate(EntityValidationContext entityValidationContext, InternalPropertyEntry property) +69 System.Data.Entity.Internal.Validation.EntityValidator.Validate(EntityValidationContext entityValidationContext) +17 System.Data.Entity.Internal.InternalEntityEntry.GetValidationResult(IDictionary
2 项目)+193 System.Data.Entity.DbContext.ValidateEntity(DbEntityEntry entityEntry,IDictionary2 items) +40 System.Data.Entity.DbContext.GetValidationErrors() +210 System.Data.Entity.Internal.InternalContext.SaveChanges() +44 System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +33 System.Data.Entity.DbContext.SaveChanges() +20 Ramazan.TestIslem.yayinDegistir(Int32 id) in D:\Projects\Visual Studio 2010\Projects\hayatdestek\hayatdestek\Ramazan\Testler.cs:102 hayatdestek.Controllers.YonetimController.YayinDegistir(Int32 id) in D:\Projects\Visual Studio 2010\Projects\hayatdestek\hayatdestek\Controllers\YonetimController.cs:180 lambda_method(Closure , ControllerBase , Object[] ) +97 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary
2个参数)+182 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary2 parameters) +27 System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28 System.Web.Mvc.Async.<>c__DisplayClass8
1.b__7(IAsyncResult) )+10 System.Web.Mvc.Async.WrappedAsyncResult1.End() +50 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +58 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +225 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult
1.End()+50 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult的 asyncResult)+34 System.Web.Mvc.Async&LT;。&以及c _DisplayClass2a.b_ 20() +24 System.Web.Mvc.Async&LT;。&以及c _DisplayClass25.b_ 22(IAsyncResult的 asyncResult)+99 System.Web.Mvc.Async.WrappedAsyncResult1.End() +50 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult
1.End()+55 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+39 System.Web.Mvc.Async&LT;。&以及c _DisplayClass4.b_ 3(IAsyncResult的 ar)+23 System.Web.Mvc.Async.WrappedAsyncResult1.End() +55 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__4(IAsyncResult asyncResult) +25 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult
1.End()+55 System.Web.Mvc。&lt;&gt; c _DisplayClasse.b_ d()+31 System.Web.Mvc.SecurityUtil.b _0(动作f)+7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)+23 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+59 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult的 结果)+9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 9629296 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155
来自@Pawel的解决方案
此异常似乎源自[EnumDataType(typeof(int), ErrorMessage =“{0}mutlakasunıolmalıdır!”)]类型应该在哪里 一个枚举类型而不是简单类型。尝试删除此属性 测试工作。请注意,EF不使用EnumDataType属性 这个问题可能与EF无关。
答案 0 :(得分:0)
你的筹码追踪:
[InvalidOperationException:'System.Int32'类型需要表示 枚举类型。]
System.ComponentModel.DataAnnotations.EnumDataTypeAttribute.IsValid(对象
建议这个注释:
[EnumDataType(typeof(int), ErrorMessage = "{0} mutlaka sayı olmalıdır!")]
<{1}}上的正在呕吐。