当我从Visual Studio中运行带有EF6和MySQL数据库的项目Web API时,运行正常,但是当我发布到本地IIS服务器时,它将返回以下异常。

System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

DataBaseLayer.Banco: : EntityType 'Banco' has no key defined. Define the key for this EntityType.
[...]
Bancos: EntityType: EntitySet 'Bancos' is based on type 'Banco' that has no keys defined.
[...]

   em System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
   em System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   em System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   em System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   em System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   em System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   em System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   em System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   em System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   em System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   em System.Data.Entity.QueryableExtensions.SingleOrDefaultAsync[TSource](IQueryable`1 source, Expression`1 predicate, CancellationToken cancellationToken)
   em System.Data.Entity.QueryableExtensions.SingleOrDefaultAsync[TSource](IQueryable`1 source, Expression`1 predicate)
   em DataBaseLayer.UtilizadorDB.d__15.MoveNext()
--- Fim do rastreio da pilha da localização anterior em que a excepção foi emitida ---
   em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   em BusinessLayer.UtilizadorBL.d__d.MoveNext()
--- Fim do rastreio da pilha da localização anterior em que a excepção foi emitida ---
   em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   em ColetorDividas.Providers.UserStoreService.d__0.MoveNext()
--- Fim do rastreio da pilha da localização anterior em que a excepção foi emitida ---
   em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   em Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter`1.GetResult()
   em Microsoft.AspNet.Identity.UserManager`2.d__12.MoveNext()
--- Fim do rastreio da pilha da localização anterior em que a excepção foi emitida ---
   em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   em ColetorDividas.Providers.ApplicationOAuthProvider.d__0.MoveNext()
--- Fim do rastreio da pilha da localização anterior em que a excepção foi emitida ---
   em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   em Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerHandler.d__3f.MoveNext()
--- Fim do rastreio da pilha da localização anterior em que a excepção foi emitida ---
   em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   em Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerHandler.d__22.MoveNext()
--- Fim do rastreio da pilha da localização anterior em que a excepção foi emitida ---
   em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   em Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerHandler.d__0.MoveNext()
--- Fim do rastreio da pilha da localização anterior em que a excepção foi emitida ---
   em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   em Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware`1.d__0.MoveNext()
--- Fim do rastreio da pilha da localização anterior em que a excepção foi emitida ---
   em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   em Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.d__5.MoveNext()
--- Fim do rastreio da pilha da localização anterior em que a excepção foi emitida ---
   em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   em Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.d__2.MoveNext()
--- Fim do rastreio da pilha da localização anterior em que a excepção foi emitida ---
   em Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar)
   em Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar)
   em System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
   em System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

I've the key Mapped with data annotation like this code:

public class Banco
{
    [Key]
    [Display(Name = ConstResources.CodigoBanco, ResourceType = typeof(ResourcePT))]
    public int CodigoBanco { get; set; }

    [Display(Name = ConstResources.Designacao, ResourceType = typeof(ResourcePT))]
    [Required(ErrorMessageResourceName = ConstResources.CampoObrigatorio, ErrorMessageResourceType = typeof(ResourcePT))]
    [StringLength(250, ErrorMessageResourceName = ConstResources.CampoTamanho, ErrorMessageResourceType = typeof(ResourcePT))]
    public string Designacao { get; set; }

    public virtual ICollection<BancoPag> BancosPagamentos { get; set; }
}


任何想法如何解决此问题。

最佳答案

IIS服务器无法识别模型中的数据注释,它仅与Fluent API一起使用。

为什么IIS服务器无法识别数据注释?

关于c# - 使用MySQL数据库生成模型时检测到EF6验证错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30150237/

10-11 02:45
查看更多