[英]Have Entity Framework code first database in MVC project. Added console project to solution but it cannot connect to database
I have an MVC project in which I used Entity Framework code first approach to create my database. 我有一个MVC项目,在其中我使用实体框架代码优先方法来创建数据库。 The database exists and I can query it from my MVC project without any issues.
该数据库已存在,我可以从我的MVC项目中查询它,而不会出现任何问题。
Now, I added a console project to the solution. 现在,我在解决方案中添加了一个控制台项目。 Within this console project I added a reference to my MVC project, added Entity Framework NuGet package and added the connection string from web.config (in the MVC project) into app.config (in the console project).
在此控制台项目中,我添加了对我的MVC项目的引用,添加了Entity Framework NuGet包,并将连接字符串从web.config(在MVC项目中)添加到app.config(在控制台项目中)。
The connection string is: 连接字符串为:
<connectionStrings>
<add name="TradeManagerContext" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=TradeManagerDB; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|TradeManagerDB.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
I verified that this is the connection string being used in the console app with this debugging code: 我验证了以下调试代码是否是控制台应用程序中使用的连接字符串:
System.Console.WriteLine("ConnectionString=" + db.Database.Connection.ConnectionString);
When my console app attempts to query the database: 当我的控制台应用程序尝试查询数据库时:
var result = db.Trades.Where(c => c.Ticker == "AAPL");
That line throws an exception with message: 该行引发异常消息:
Cannot attach the file 'C:\\Users\\RED\\documents\\visual studio 2015\\Projects\\TradeManager\\Console\\bin\\Debug\\TradeManagerDB.mdf' as database 'TradeManagerDB'.
无法将文件'C:\\ Users \\ RED \\ documents \\ Visual Studio 2015 \\ Projects \\ TradeManager \\ Console \\ bin \\ Debug \\ TradeManagerDB.mdf附加为数据库'TradeManagerDB'。
Any ideas on what this exception means and how I can resolve it? 关于此异常的含义以及如何解决的任何想法?
Here is the full stack trace: 这是完整的堆栈跟踪:
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(的DbConnection owningObject,UInt32的waitForMultipleObjectsTimeout,布尔allowCreate,布尔onlyOneCheckConnection,DbConnectionOptions USEROPTIONS,DbConnectionInternal&连接)在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(的DbConnection owningObject,TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1重试,DbConnectionOptions USEROPTIONS,DbConnectionInternal oldConnection,DbConnectionInternal&连接)在System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(的DbConnection outerConnection,DbConnectionFactory connectionFactory的,TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry) at System.Data.SqlClient.SqlConnection.Open() at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__36(DbConnection t, DbConnectionInterceptionContext c) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action
2 operation, TInterceptionContext interceptionContext, Action3 executing, Action
3 executed) at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) at System.Data.Entity.Core.EntityClient.EntityConnection.b__2()1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
重试1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1,重试System.Data.SqlClient.SqlConnection.TryOpenInner(DbConnectionOptions userOptions)(1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1重试)System.Data.SqlClient.SqlConnection.Open( )在System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__36(DbConnection t,DbConnectionInterceptionContext c)在System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action
2操作,在System.Data.Entity.Core.EntityClient.EntityConnection.b__2()处3 executing, Action
TInterceptionContext拦截上下文,3 executing, Action
动作3 executing, Action
3)(System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection连接,DbInterceptionContext拦截上下文)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.b__0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) at System.Data.Entity.Core.EntityClient.EntityConnection.Open()在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy。<> c__DisplayClass1.b__0()在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func`1操作)在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy。在System.Data.Entity.Core.EntityClient.EntityConnection.Open()处执行(动作操作)
It is trying to load the file TradeManagerDB from the App_Data folder. 它正在尝试从App_Data文件夹加载文件TradeManagerDB。 You need to use a different syntax to load it from elsewhere.
您需要使用其他语法从其他位置加载它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.