异常信息如下:
发生了 System.Data.Entity.Infrastructure.DbUpdateException
HResult=0x80131501
Message=更新条目时出错。有关详细信息,请参见内部异常。
Source=EntityFramework
StackTrace:
在 System.Data.Entity.Internal.InternalContext.SaveChanges()
在 System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
在 System.Data.Entity.DbContext.SaveChanges()
在 GMS.Framework.DAL.DbContextBase.SaveChanges() 在 C:\GMS1\Src\GMS.Framework.DAL\DbContextBase.cs 中: 第 82 行
在 GMS.Framework.DAL.DbContextBase.Insert[T](T entity) 在 C:\GMS1\Src\GMS.Framework.DAL\DbContextBase.cs 中: 第 49 行
在 GMS.Account.BLL.AccountService.SaveVerifyCode(String verifyCodeText) 在 C:\GMS1\Src\GMS.Account.BLL\AccountService.cs 中: 第 237 行
在 Castle.Proxies.Invocations.IAccountService_SaveVerifyCode.InvokeMethodOnTarget()
在 Castle.DynamicProxy.AbstractInvocation.Proceed()
在 GMS.Core.Service.InvokeInterceptor.Intercept(IInvocation invocation) 在 C:\GMS1\Src\GMS.Core.Service\ServiceHelper.cs 中: 第 64 行
在 Castle.DynamicProxy.AbstractInvocation.Proceed()
在 Castle.Proxies.IAccountServiceProxy.SaveVerifyCode(String verifyCodeText)
在 GMS.Web.VerifyCodeHelper.SaveVerifyCode(String verifyCode) 在 C:\GMS1\Src\GMS.Web\VerifyCodeHelper.cs 中: 第 10 行
在 GMS.Web.Admin.Areas.Account.Controllers.CommonController.VerifyImage() 在 C:\GMS1\Src\GMS.Web.Admin\Areas\Account\Controllers\CommonController.cs 中: 第 16 行
在 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
在 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
在 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
在 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
内部异常 1:
UpdateException: 更新条目时出错。有关详细信息,请参见内部异常。
内部异常 2:
SqlException: 对象名 'dbo.VerifyCodes' 无效。
注意:这里主要是内部异常2,SqlException对象名'dbo.VerifyCodes'无效的异常的解决方案.
1、模型类如下,注意模型类中的类名VerifyCode,Table属性中也是为VerifyCode
using System;
using GMS.Framework.Contract;
using System.ComponentModel.DataAnnotations.Schema;
namespace GMS.Account.Contract
{
[Serializable]
[Table("VerifyCode")]
public class VerifyCode : ModelBase
{
public Guid Guid { get; set; }
public string VerifyText { get; set; }
}
}
2、查找到数据库中的表,注意数据库中的表名也是VerifyCode
3、从以上两步我们就了解到不是模型类名与数据库表名的问题,那么就是我们真正的解决方案了:
找到DbContext类的派生类,然后重写OnModelCreating方法
本案例中代码如下:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<AccountDbContext>(null);
modelBuilder.Entity<User>()
.HasMany(e => e.Roles)
.WithMany(e => e.Users)
.Map(m =>
{
m.ToTable("UserRole");
m.MapLeftKey("UserID");
m.MapRightKey("RoleID");
});
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();//注意这句话就是我们真正要添加的,可以防止自动加s
}
到这里,我们就大功告成了