简体   繁体   English

EF6代码优先:更新数据库登录失败

[英]EF6 Code First: Login Failing on update-database

I am trying to do an update-database on an EF6 code first database. 我正在尝试在EF6代码优先数据库上进行更新。 All I've done is build my models and the datacontext, run enable-migrations and did an add-migration InitialCreate. 我要做的就是构建模型和数据上下文,运行enable-migrations,并进行add-migration InitialCreate。 That all worked well. 一切都很好。

But the update-database is failing in two different ways. 但是更新数据库以两种不同的方式失败。

For a while I had it working, but it was creating the database files (ie, the MDF and log files) under c:\\Users\\. 有一段时间我可以使用它,但是它正在c:\\ Users \\下创建数据库文件(即MDF和日志文件)。 I don't want them there. 我不要他们在那里。 I want them in the App_Data folder of an MVC website I'm building (the database schema is in a separate class library DLL because it needs to be accessible to both the MVC app and some data import and massaging stuff in a console app). 我希望它们位于我正在构建的MVC网站的App_Data文件夹中(数据库架构位于单独的类库DLL中,因为MVC应用程序以及控制台应用程序中的某些数据导入和按摩操作都需要访问它)。

I tried to change the location of the database files by tweaking the connection string, but my changes were either ignored (ie, the database files keep being created at c:\\Users\\) or I ran into the second error, "Cannot open database ... requested by the login...blah blah blah". 我试图通过调整连接字符串来更改数据库文件的位置,但是我的更改被忽略(即,数据库文件始终在c:\\ Users \\上创建),或者我遇到第二个错误,“无法打开数据库...登录要求...等等等等“。 Yet I could open the database in SSMS, and my Windows user account is the owner of the database. 但是我可以在SSMS中打开数据库,而我的Windows用户帐户是数据库的所有者。

For fun I tried deleting and creating the database from within SSMS. 为了好玩,我尝试从SSMS内删除和创建数据库。 That went fine...but the update-database command >>still<< fails to log in. 一切正常...但是update-database命令>> still <<无法登录。

Any help in unraveling this mess would be much appreciated! 在解开这个混乱的任何帮助将不胜感激!

The constructor for my datacontext: 我的datacontext的构造函数:

public CampaignDbContext()
    : base( "Council2015", throwIfV1Schema: false )
{
    Database.SetInitializer<CampaignDbContext>( null );
}

The connection string in the class library app.config: 类库app.config中的连接字符串:

<connectionStrings>
    <add name="Council2015" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=E:\\Programming\\Council2015\\Council2015\\App_Data\\Council2015.mdf;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/>
</connectionStrings>

The most recent exception thrown by update-database: 更新数据库抛出的最新异常:

Applying explicit migrations: [201503180506326_InitialCreate].
Applying explicit migration: 201503180506326_InitialCreate.
System.Data.SqlClient.SqlException (0x80131904): Cannot open database "Council2015" requested by the login. The login failed.
Login failed for user 'KENSEI\Mark'.
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
...
Cannot open database "Council2015" requested by the login. The login failed.
Login failed for user 'KENSEI\Mark'.

Additional Info: 附加信息:

There's something I obviously don't understand about connection strings. 我显然对连接字符串不了解。 I can get update-database to work if I give the connection string a new name (eg, Council2015a) and change the corresponding constructor parameter in CampaignDbContext. 如果给连接字符串起一个新名称(例如,Council2015a)并在CampaignDbContext中更改相应的构造函数参数,我可以使update-database工作。 But it creates a new database -- under c:\\Users\\, where I don't want it -- instead of attaching to the existing database file. 但是它创建了一个新的数据库-在c:\\ Users \\下,我不想要它-而不是附加到现有的数据库文件。

Moreover, if you delete the database files, or move them (and update the attach file parameter), once again the same login failure occurs. 此外,如果删除数据库文件或移动它们(并更新附加文件参数),将再次发生相同的登录失败。

So something somewhere is remembering the previously-created databases, and "refusing" to let go. 因此,某些地方会记住先前创建的数据库,并“拒绝”放弃。

This is all massively counter-intuitive (including why the database is being named after the name parameter in the connection string). 这完全违反直觉(包括为什么在连接字符串中使用name参数命名数据库的原因)。

I stumbled across the answer to the basic problem -- how to get the database created where you want it -- which appears to have solved the other issues I was encountering. 我偶然发现了基本问题的答案-如何在所需位置创建数据库-看来已经解决了我遇到的其他问题。

Use a connection string that looks like you would expect it to have to look: 使用看起来像您期望的连接字符串:

<add name="Council2015" connectionString="Server=(LocalDB)\v11.0; Integrated Security=true; AttachDbFileName=E:\Programming\Council2015\Council2015\App_Data\Council2015.mdf" providerName="System.Data.SqlClient"/>

But when you run update-database (after doing whatever add-migration calls you need to do), make sure the connection string will be found by the update-database command . 但是,当您运行update-database时(执行所需的任何add-migration调用之后),请确保通过update-database命令找到了连接字符串

That sounds like it should happen automatically. 听起来应该自动发生。 And it does, if you're creating your database in the startup project for your solution. 如果要在解决方案的启动项目中创建数据库,则可以。 Because that's where update-database looks for connection strings, by default. 因为默认情况下,更新数据库会在此处查找连接字符串。

In my case I was defining the data classes in a class library project so that I could use it in multiple application projects. 就我而言,我在类库项目中定义数据类,以便可以在多个应用程序项目中使用它。 So update-database was looking in the wrong place for connection strings when I ran it within the "context" of the class library project. 因此,当我在类库项目的“上下文”中运行连接数据库时,更新数据库在错误的位置查找连接字符串。

Selecting the class library as the "Default project" in the Package Manager Console doesn't, despite what you (and I) might think, inform update-database's search process for connection strings. 尽管您(和我)可能会想到,但在Package Manager控制台中将类库选择为“默认项目”并不会通知更新数据库的连接字符串搜索过程。

Instead, specify the startup project explicitly: 相反,应明确指定启动项目:

update-database -StartUpProjectName "<class library project name, in my case>"

and voila! 和瞧!

Now as to why a failure to locate the correct connection string -- since the id name I was using in the class library project didn't even appear in the list of connection strings in my solution's startup project -- should have resulted in a "user login failure" error...well, that's beyond me :). 现在,为什么无法找到正确的连接字符串(由于我在类库项目中使用的id名称甚至没有出现在解决方案的启动项目的连接字符串列表中),都应该导致“用户登录失败”错误...好吧,这超出了我:)。 And an example of lousy software design, IMHO. 还有糟糕的软件设计示例,恕我直言。

在连接字符串中,尝试添加数据库用户名和密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM