在使用MySQL数据库生成模型期间检测到EF6验证错误

时间:2015-05-10 10:21:37

标签: c# mysql entity-framework asp.net-web-api

当我从Visual Studio运行它时,我的项目web api与EF6和MySQL数据库运行正常,但当我发布到我的本地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()

我使用数据注释映射的键如下代码:

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; }
}

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

1 个答案:

答案 0 :(得分:0)

IIS服务器无法识别模型中的数据注释,只能使用Fluent API。

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