Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.1 code first causes weird (login) runtime errors

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! 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: ]

like image 541
Savvas Sopiadis Avatar asked May 05 '11 20:05

Savvas Sopiadis


2 Answers

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.

like image 80
onur dulgeroglu Avatar answered Nov 23 '22 09:11

onur dulgeroglu


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!

like image 22
Savvas Sopiadis Avatar answered Nov 23 '22 07:11

Savvas Sopiadis