开发者

EF 4.1 code first causes weird (login) runtime errors

开发者 https://www.devze.com 2023-03-03 11:57 出处:网络
I\'m using EF 4.1 code first. Running into a very weird situation: Database does not exist, code is executed and as soon as the code wants to execute a query against the repository (using also reposit

I'm using EF 4.1 code first. Running into a very weird situation: Database does not exist, code is executed and as soon as the code wants to execute a query against the repository (using also repository pattern)

MyRepsitory.Get(whereClause)

i'm getting this error:

Cannot open database "MyDatabase" requested by the login. The login failed. Login failed for user 'sa'.

OK! 开发者_Go百科So i tried to find what it is caused by and did the following: set a break point at the line which caused the error and as soon as this line was hit, i issued from the debugging (watch) window another query against the repository

MyRepository.GetAll();

et voila the database gets created. So i thought: why not trick the database (as a workaround) and issue this request against the repository? The result: i get the same error as soon as this gets executed in code!

What am i doing wrong? This used to work like a charm!

EDIT This drives me mad! I isolated the model and some code (for the repository pattern) into a new project. Tried to execute the same code and it works in this new project. How is that possible?

Looked in the logs of SQL Serv2008 Exp and i see the following errors/events:

Starting up database 'MyDatabase'

Setting database option SINGLE_USER to ON for database MyDatabase.

Error: 18456, Severity: 14, State: 38.

Login failed for user 'sa'. Reason: Failed to open the explicitly specified database. [CLIENT: ]


thanks Savvas, for sharing the fact that creating a new solution solved your problem. I faced the same weird issue with EF 4.1, and deleting the VS2010 "SUO" file seemed to do the trick for me. Thought I'd share this to save others the time to recreate the solution from scratch.


For the sake of future use: it turns out that EF 4.1 was not the source of my problems!

As i stated above (after the EDIT) it is possible to run the (UNCHANGED) code in a new project and it will execute as expected!

It is also true that i successfully created a new project, referenced the model and the DAL (repositories, etc) of the original project and the new code in the new project executed as expected.

This lead me to think that this is a Visual Studio 2010 Solution(or Project)-based problem. I cannot remember having done anything weird to this solution (the only thing i did was adding a test-project, which i afterward deleted, and also some - i think three files - related to this project test-project - IS THIS WEIRD?!).

The only solution i could think of, was to create a new Visual Studio 2010 Solution from scratch and place the original Projects in the solution tree...and it worked!


I ran into this and after digging around a bit have discovered it's a problem with Code First thinking it's the sole owner of the schema.

http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/79885a6d-e0f2-4948-8f3c-d5da1941ace3

They havent quite got it figured out for user instances of the DB (that's where 'User Instance = true' is in your connection string and you create an SQL Server file in your App_Data folder). So either remove it or use the following:

Data Source=.\SQLEXPRESS;Database=NerdDinners;Integrated Security=SSPI;

Code First will create and attach (or modify) a database directly to your instance of SQL Server. You can then connect to it using Management Studio or VS Server Explorer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号