EF 4.1代码首先导致出现奇怪(登录)运行时错误 [英] EF 4.1 code first causes weird (login) runtime errors

查看:432
本文介绍了EF 4.1代码首先导致出现奇怪(登录)运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用EF 4.1代码。运行到一个非常奇怪的情况:
数据库不存在,代码被执行,一旦代码想要对存储库执行查询(也使用存储库模式)

  MyRepsitory.Get(whereClause)

m得到这个错误:


无法打开登录请求的数据库MyDatabase
。登录
失败。登录用户'sa'失败。


确定!所以我试图找到它是由什么造成的,并做了以下:设置一个断点引起错误的行,只要这一行被命中,我从调试(watch)窗口发出另一个查询对库

  MyRepository.GetAll(); 

等于数据库被创建。所以我想:为什么不欺骗数据库(作为一种解决方法),并针对存储库发出此请求?结果:我得到相同的错误,一旦这被执行代码!



我做错了什么?
这个以前的工作就像一个魅力!



编辑
这让我疯狂!
我将模型和一些代码(用于存储库模式)隔离到一个新项目中。试图执行相同的代码,它在这个新的项目中工作。如何可能?



查看SQL Serv2008 Exp的日志,我看到以下错误/事件:



启动数据库'MyDatabase'



为数据库MyDatabase设置数据库选项SINGLE_USER为ON。



错误: 18456,严重性:14,州:38。



用户'sa'登录失败。原因:无法打开明确指定的数据库。 [客户:]

解决方案

为了将来使用:事实证明,EF 4.1不是我的问题的根源!



如上所述(EDIT之后),可以在新项目中运行(UNCHANGED)代码,并按预期执行



我也成功地创建了一个新项目,引用了原始项目的模型和DAL(存储库等),并按照预期执行了新项目中的新代码。



这导致我认为这是一个基于Visual Studio 2010解决方案(或Project)的问题。我不记得这个解决方案有什么奇怪的事情(我唯一做的是添加一个测试项目,我以后删除,还有一些 - 我认为三个文件 - 与这个项目测试项目相关 - 这是WEIRD吗? )



我可以想到的唯一解决方案是从头开始创建一个新的Visual Studio 2010解决方案,并将原始项目放在解决方案树中...工作!


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

解决方案

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!

这篇关于EF 4.1代码首先导致出现奇怪(登录)运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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