2017-09-07 37 views
0

我在此时遇到了EntityFramework问题。 我在数据库上下文中添加了一些新的实体,其中一个是客户端。这里是我的的DbContext新条目:实体类型不是当前上下文模型的一部分(autofac)

public class DatabaseContext : IdentityDbContext<User> 
{ 

    //Define our tables 
    public DbSet<Email> Emails { get; set; } 
    public DbSet<Log> Logs { get; set; } 
    public DbSet<Setting> Settings { get; set; } 
    public DbSet<Models.Message> Messages { get; set; } 
    public DbSet<HealthCheck> HealthChecks { get; set; } 

    public DbSet<Client> Clients { get; set; } 
    public DbSet<ClientClaim> ClientClaims { get; set; } 
    public DbSet<ClientSecret> ClientSecrets { get; set; } 
    public DbSet<Consent> Consents { get; set; } 
    public DbSet<Scope> Scopes { get; set; } 
    public DbSet<ScopeClaim> ScopeClaims { get; set; } 

    /// <summary> 
    /// Default constructor 
    /// </summary> 
    public DatabaseContext(CormarConfig config) : base(config.SqlConnectionString) 
    { 

     // Write our SQL to the debug window 
     Database.Log = s => Debug.WriteLine(s); 

     // Disable Lazy Loading 
     base.Configuration.LazyLoadingEnabled = false; 

     // TODO: Remove when publishing! 
     Database.SetInitializer<DatabaseContext>(null); 
    } 

    /// <summary> 
    /// Overrides the inherited OnModelCreated method. 
    /// </summary> 
    /// <param name="modelBuilder">The DbModelBuilder</param> 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 

     // Rename tables 
     modelBuilder.Entity<IdentityRole>().ToTable("Roles"); 
     modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles"); 
     modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims"); 
     modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins"); 
     modelBuilder.Entity<Models.Message>().ToTable("Messages"); 

     // Create our foreign key 
     modelBuilder.Entity<IdentityRole>().HasMany(m => m.Users).WithRequired().HasForeignKey(m => m.RoleId); 

     // Create our primary keys 
     modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(m => m.UserId); 
     modelBuilder.Entity<IdentityRole>().HasKey<string>(m => m.Id); 
     modelBuilder.Entity<IdentityUserRole>().HasKey(m => new { m.RoleId, m.UserId }); 
     modelBuilder.Entity<Setting>().HasKey(m => new { m.Id, m.Name }); 

     //modelBuilder.Entity<RefreshToken>().Property(t => t.Subject).HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_SubjectClient", 0) { IsUnique = true })); 
     //modelBuilder.Entity<RefreshToken>().Property(t => t.ClientId).HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_SubjectClient", 1) { IsUnique = true }));    
    } 
} 

我已经运行add-migrationupdate-database,可以看到我的新的实体中创建表。 然后,我创建了一个简单的类,应该公开客户,它看起来像这样:

public class IdentityServerClientStore : IClientStore 
{ 
    private readonly DbSet<Client> _clients; 

    public IdentityServerClientStore(DbContext context) 
    { 
     _clients = context.Set<Client>(); 
    } 

    public async Task<Client> FindClientByIdAsync(string clientId) => await _clients.SingleOrDefaultAsync(m => m.ClientId.Equals(clientId)); 
} 

这是使用Autofac注入注册这样的:

builder.RegisterType<DatabaseContext>().As<DbContext>().InstancePerDependency(); 
builder.RegisterType<IdentityServerClientStore>().As<idsrv.IClientStore>().InstancePerDependency(); 

但是当我尝试做任何事情,我得到这个错误:

"The entity type Client is not part of the model for the current context."

整个堆栈跟踪看起来像这样:

{ 
    "Message": "An error has occurred.", 
    "ExceptionMessage": "The entity type Client is not part of the model for the current context.", 
    "ExceptionType": "System.InvalidOperationException", 
    "StackTrace": " at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)\r\n at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\r\n at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()\r\n at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()\r\n at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()\r\n at System.Data.Entity.QueryableExtensions.SingleOrDefaultAsync[TSource](IQueryable`1 source, Expression`1 predicate, CancellationToken cancellationToken)\r\n at System.Data.Entity.QueryableExtensions.SingleOrDefaultAsync[TSource](IQueryable`1 source, Expression`1 predicate)\r\n at Cormar.Business.Identity.IdentityServerClientStore.<FindClientByIdAsync>d__2.MoveNext() in C:\\Users\\JaymieJeffrey\\Documents\\GitHub\\Cormar\\Cormar.Business\\Identity\\IdentityServerClientStore.cs:line 18\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at IdentityServer3.Core.Validation.ClientSecretValidator.<ValidateAsync>d__2.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Validation\\ClientSecretValidator.cs:line 63\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at IdentityServer3.Core.Endpoints.TokenEndpointController.<ProcessAsync>d__7.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Endpoints\\Connect\\TokenEndpointController.cs:line 98\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at IdentityServer3.Core.Endpoints.TokenEndpointController.<Post>d__0.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Endpoints\\Connect\\TokenEndpointController.cs:line 74\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.System.Web.Http910911.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Tracing.Tracers.HttpControllerTracer.<ExecuteAsyncCore>d__5.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext() in c:\\ballen\\github\\identity\\IdSrv3\\IdentityServer3\\source\\Core\\Configuration\\Hosting\\NoCacheAttribute.cs:line 0" 
} 

任何人都可以猜测为什么这可能会发生?

+0

你能告诉你的'Client'类? – IvanJazz

+0

它是IdentityServer3的一部分 – r3plica

回答

0

我想你只需要添加到您的OnModelCreating:

modelBuilder.Entity<Client>().ToTable("Client"); 
+1

只有在需要重命名自动生成的表时才需要它吗?如果我这样做,它会将表格“客户”重命名为“客户”。 – r3plica

+0

您正在将DbSet属性名称定义为Clients(复数)。如果你的表名是复数,那可以正常工作。但是,错误是指出Client(单数)不是当前上下文的一部分。我相信你是在它上面添加它应该为你工作的线。 – ovation22

+0

我试过了,它没有工作。我仍然得到相同的错误:( – r3plica

相关问题