如何将aspnet.identity.entityframework分离到自己的程序集

时间:2014-06-22 12:50:04

标签: entity-framework authentication asp.net-mvc-5 decouple

我正在尝试将appicationDBContext从asp.net MVC5 Web应用程序项目移到自己的程序集中。我尝试为数据访问层创建一个类库,并使用nuget来安装aspnet.identity程序集。

我无法让这个工作。我尝试过几个不同的例子,但不确定最好的方法是什么。

我在codeplex中创建了一个基本示例: https://testdal.codeplex.com/

这是一个空的mvc5网站,引用了类库DAL。在DAL中是来自默认样本网站的模型以及参考和一些基本测试代码,以尝试注册用户。

我收到以下错误:

Instance failure.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 Exception Details: System.InvalidOperationException: Instance failure.

Source Error:



Line 18:
Line 19:             var user = new ApplicationUser() { UserName = "paul" };
Line 20:             var result = UserManager.Create(user, "123456");
Line 21:
Line 22:


 Source File:  c:\Users\paul\Documents\Visual Studio 2013\Projects\emptyWebsite\DAL\Register.cs    Line:  20

Stack Trace:



[InvalidOperationException: Instance failure.]
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +414
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +196
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +146
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +16
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +94
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +110
   System.Data.SqlClient.SqlConnection.Open() +96
   System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<open>
    b__36(DbConnection t, DbConnectionInterceptionContext c) +36
    System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +138
    System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) +477
    System.Data.Entity.SqlServer.<>c__DisplayClass34.<usingconnection>
        b__32() +344
        System.Data.Entity.SqlServer.<>c__DisplayClass1.<execute>
            b__0() +34
            System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +251
            System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) +196
            System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action`1 act) +420
            System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act) +335
            System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable`1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript) +151
            System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection) +287
            System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection) +110
            System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase() +236
            System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection) +143
            System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +207
            System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +42
            System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +136
            System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() +33
            System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext) +175
            System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState) +150
            System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) +444
            System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) +288
            System.Data.Entity.Internal.<>c__DisplayClassf`1.<createinitializationaction>
                b__e() +165
                System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +110
                System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +660
                System.Data.Entity.Internal.LazyInternalContext.<initializedatabase>
                    b__4(InternalContext c) +31
                    System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +143
                    System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +292
                    System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +123
                    System.Data.Entity.Internal.InternalContext.Initialize() +42
                    System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +39
                    System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +137
                    System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path) +41
                    System.Data.Entity.Infrastructure.DbQuery`1.Include(String path) +142
                    System.Data.Entity.QueryableExtensions.Include(IQueryable`1 source, String path) +205
                    System.Data.Entity.QueryableExtensions.Include(IQueryable`1 source, Expression`1 path) +305
                    Microsoft.AspNet.Identity.EntityFramework.UserStore`6.GetUserAggregateAsync(Expression`1 filter) +610
                    Microsoft.AspNet.Identity.EntityFramework.UserStore`6.FindByNameAsync(String userName) +1069
                    Microsoft.AspNet.Identity.UserManager`2.FindByNameAsync(String userName) +177
                    Microsoft.AspNet.Identity.<validateusername>
                        d__4.MoveNext() +803
                        System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
                        System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
                        System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
                        Microsoft.AspNet.Identity.<validateasync>
                            d__0.MoveNext() +468
                            System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
                            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
                            System.Runtime.CompilerServices.ConfiguredTaskAwaiter.GetResult() +24
                            Microsoft.AspNet.Identity.<createasync>
                                d__0.MoveNext() +846
                                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
                                System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
                                System.Runtime.CompilerServices.ConfiguredTaskAwaiter.GetResult() +24
                                Microsoft.AspNet.Identity.<createasync>
                                    d__d.MoveNext() +1113
                                    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
                                    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
                                    System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
                                    Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func`1 func) +228
                                    Microsoft.AspNet.Identity.UserManagerExtensions.Create(UserManager`2 manager, TUser user, String password) +260
                                    DAL.Register.register() in c:\Users\paul\Documents\Visual Studio 2013\Projects\emptyWebsite\DAL\Register.cs:20
                                    emptyWebsite.Controllers.HomeController.Index() in c:\Users\paul\Documents\Visual Studio 2013\Projects\emptyWebsite\emptyWebsite\Controllers\HomeController.cs:20
                                    lambda_method(Closure , ControllerBase , Object[] ) +101
                                    System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
                                    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +435
                                    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
                                    System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76
                                    System.Web.Mvc.Async.AsyncControllerActionInvoker.<begininvokesynchronousactionmethod>
                                        b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36
                                        System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +73
                                        System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
                                        System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
                                        System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
                                        System.Web.Mvc.Async.AsyncInvocationWithFilters.<invokeactionmethodfilterasynchronouslyrecursive>
                                            b__3c() +117
                                            System.Web.Mvc.Async.<>c__DisplayClass45.<invokeactionmethodfilterasynchronouslyrecursive>
                                                b__3e() +323
                                                System.Web.Mvc.Async.<>c__DisplayClass30.<begininvokeactionmethodwithfilters>
                                                    b__2f(IAsyncResult asyncResult) +44
                                                    System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
                                                    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
                                                    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
                                                    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
                                                    System.Web.Mvc.Async.<>c__DisplayClass28.<begininvokeaction>
                                                        b__19() +72
                                                        System.Web.Mvc.Async.<>c__DisplayClass1e.<begininvokeaction>
                                                            b__1b(IAsyncResult asyncResult) +185
                                                            System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
                                                            System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
                                                            System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
                                                            System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
                                                            System.Web.Mvc.Controller.<beginexecutecore>
                                                                b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
                                                                System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
                                                                System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
                                                                System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
                                                                System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
                                                                System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
                                                                System.Web.Mvc.Controller.<beginexecute>
                                                                    b__15(IAsyncResult asyncResult, Controller controller) +39
                                                                    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
                                                                    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
                                                                    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
                                                                    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
                                                                    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
                                                                    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
                                                                    System.Web.Mvc.MvcHandler.<beginprocessrequest>
                                                                        b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +39
                                                                        System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
                                                                        System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
                                                                        System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
                                                                        System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
                                                                        System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
                                                                        System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
                                                                        System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514928
                                                                        System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

有人可以帮我找到一种从UI项目中分离模型的简单方法吗?

由于

1 个答案:

答案 0 :(得分:0)

尝试以下后我设法解决了这个问题:

Moving ASP.NET Identity model to class library http://www.umbraworks.net/bl0g/rebuildall/2013/10/22/Moving_ASP_NET_Identity_model_into_another_assembly

我最初尝试了这个并尝试了许多类似的方法,虽然我发现从新鲜开始,删除数据库并指定特定的数据库位置帮助。我后来认为可能启用数据迁移可以解决问题。这仍然取决于web.config中的连接字符串,因此不会完全解耦,但现在似乎正在工作

感谢

相关问题