我正在尝试切换列值。我正在使用实体框架数据库连接。
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.
测试者类
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(对象值,ValidationContext 验证上下文)+115 System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(对象值,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 property) +170 System.Data.Entity.Internal.Validation.PropertyValidator.Validate(EntityValidationContext entityValidationContext, InternalMemberEntry property) +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 items) +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.<>c _DisplayClass2a.b_ 20() +24 System.Web.Mvc.Async.<>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.<>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.<>c _DisplayClasse.b_ d() +31 System.Web.Mvc.SecurityUtil.b _0 (动作 f)+7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(动作动作)+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& completedSynchronously) +155
@Pawel 的解决方案
此异常似乎源自 [EnumDataType(typeof(int), ErrorMessage = "{0} mutlaka sayı olmalıdır!")],其中类型应该是 Enum 类型而不是简单类型。尝试删除此属性以测试是否有效。请注意,EF 不使用 EnumDataType 属性,这个问题可能与 EF 无关。