EF4 Code First - ConnectionString to SQL Express [英] EF4 Code First - ConnectionString to SQL Express

查看:67
本文介绍了EF4 Code First - ConnectionString to SQL Express的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试用EF4 Code First的东西。 遵循ScottGu的教程:  http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx。


效果很好SQL CE - .sdf文件第一次成功创建,随后我更改模型。 很酷。


将我的连接字符串更改为我认为适用于SQL Express数据库的内容,但它不起作用。 框架无法在第一次创建数据库时(当App_Data目录中不存在MDF文件时)。 如果我使用SQL Management Studio手动创建SQL Express
数据库,则分离(空)数据库并将文件移动到App_Data目录...它可以工作 - 能够使用该空DB作为起点并创建模型所需的表。


从那时起,Code-First框架甚至能够根据我创建的任何模型更改*更新*我的数据库模式(我甚至有RecreateDatabaseIfModelChanges类在Seed()方法中抽取样本数据。)


但是......问题仍然是第一次运行应用程序。 
它可以为我创建SQL Express数据库
。 


例外: 


System.Data.SqlClient.SqlException:无法打开数据库"NerdDinner-Express"登录请求。登录失败。

用户'< myDomainUserName>'登录失败。


 


这是我的SQL Express连接字符串 - 我应该改变什么?


< add name =" NerdDinners" connectionString =" Data Source = .\SQLEXPRESS; AttachDbFilename = | DataDirectory | \NerdDinner-Express.mdf; Initial Catalog = NerdDinner-Express; Integrated Security = True; User Instance = True; MultipleActiveResultSets = True"
providerName =" System.Data.SqlClient" />


 


解决方案

但是......问题仍然是第一次运行应用程序。 
它可以为我创建SQL Express数据库。 

异常: 


System.Data.SqlClient.SqlException:无法打开数据库"NerdDinner-Express"登录请求。登录失败。

用户'< myDomainUserName>'登录失败。


您好,


请尝试在sql server中授予帐户适当的权限。在asp.net应用程序中,您还需要授予NETWORK SERVICE的权限,默认情况下由IIS使用。


请查看此博客文章,我相信它可以帮助您:


http://blog.sqlauthority.com/2009/08/20/sql-server-fix-error-无法打开 - 数据库请求登录 - 登录 - 登录失败 - 登录失败 - 用户 - nt-authoritynetwork-service /


最佳问候


I'm giving the EF4 Code First stuff a test drive.  Following ScottGu's tutorial here:  http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx.

Works great against SQL CE -- .sdf file gets created successfully the first time, and any subsequent time I change the model.  Cool.

Change my connection string to what I think should work for SQL Express database, and it doesn't work.  The framework can't create the database the first time (when no MDF file exists in the App_Data directory).  If I manually create a SQL Express DB using SQL Management Studio, then detach the (empty) DB and move the files to the App_Data directory ... it works - able to use that empty DB as a starting point and create the tables necessary for the model.

From that point, the Code-First framework is even able to *update* my DB schema per any model changes I create (I even have the RecreateDatabaseIfModelChanges class pumping in sample data in the Seed() method).

But ... the problem is still the very first time running the app.  It CANT create the SQL Express DB for me

Exception: 

System.Data.SqlClient.SqlException: Cannot open database "NerdDinner-Express" requested by the login. The login failed.
Login failed for user '<myDomainUserName>'.

 

Here is my connection string for SQL Express - anything I should change?

<add name="NerdDinners" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NerdDinner-Express.mdf;Initial Catalog=NerdDinner-Express;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

 

?

解决方案

But ... the problem is still the very first time running the app.  It CANT create the SQL Express DB for me

Exception: 

System.Data.SqlClient.SqlException: Cannot open database "NerdDinner-Express" requested by the login. The login failed.
Login failed for user '<myDomainUserName>'.

Hello,

Please try to grant the account proper permission in sql server. In asp.net applications you also need to grant permission to NETWORK SERVICE which by default is used by IIS.

Please take a look at this blog post, I believe it could help you:

http://blog.sqlauthority.com/2009/08/20/sql-server-fix-error-cannot-open-database-requested-by-the-login-the-login-failed-login-failed-for-user-nt-authoritynetwork-service/

Best regards


这篇关于EF4 Code First - ConnectionString to SQL Express的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆