2017-09-07 40 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

相關問題