Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7909857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:37:43+00:00 2026-06-03T12:37:43+00:00

I’ve used EF 4.1 ( Code First ) in an MVC 3 project a

  • 0

I’ve used EF 4.1 (Code First) in an MVC 3 project a while back, and it was good.

Today I tried using EF 4.3.1 (Code First) in a WinForms project and encountered some real voodoo: (The original project I was working on was WinForms, however the same thing is true for the attached Console Application code.)

When trying to enter a simple class into the database, I get the following exception:
Cannot open database “Test” requested by the login. The login failed.
Login failed for user ‘[ComputerName]\[Administrator]’.

I’ve tried checking the SQL Server (2008 R2) configurations but my user does have all the permissions on the server.

NOTE: The Database does not exist when I first run the application.

Even the following sample code does not work :

class Program
{
    public static void Main()
    {
        var person = new Person { Name = "Nathan" };
        using (var db = new MyContext())
        { //#1
            db.Persons.Add(person);
            db.SaveChanges();
        }
        Console.Write("Person saved !");
        Console.ReadLine();
    }
}

public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Person> Persons { get; set; }
    public MyContext()
        : base("Data Source=localhost;Database=Test;Integrated Security=True;")
    { }
}

I’ve tried re-installing EF 4.3.1 – to no avail.
Restarting the SQL-Server also had no effect.

NOTE: I’ve noticed quite a lot of similar questions around, but none are both the same as mine and answered – here’s me hoping I’ll get lucky here 🙂

To clarify: The user being denied access is the Owner (local administrator) of the machine.

EDIT: I’ve been running a few experiments and I’ve found something quite strange:

If I pause the program at the line marked “#1” and check the value of db.Database.Exists() via the Watch window, It tells me: “The function evaluation requires all threads to run” with that little wavey button I can click to make it run all threads.
Image depicting the above paragraph

If I click on that button the function evaluates to False.

Now I can do 2 things:

  1. Continue execution: In this case I get the exact same exception.
  2. Execute db.Database.Create() from the Watch window: In this case the program runs successfully to the end and I see the Database, Table and Row created in my SQL Management Studio.

Seems like a simple solution, right? Wrong!

I’ve tried prefixing the data access with a call to db.Database.CreateIfNotExists(); but then I get the exact same exception on that line.

db.Database.Initialize(true); has no effect either…

NOTE: If by using the above method I create the database, I can use it properly from that point onwards, so that’s at least half a consolation 😛 The problem is, of course, what happens when I add a DropCreateIfModelChanges or DropCreateAlways Initalizer (since I’m in the middle of active development of the model).

Thanks.

[I’m suspecting it’s more of a Entity Framework problem than SQL Server.]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:37:44+00:00Added an answer on June 3, 2026 at 12:37 pm

    You can see this exception when debugging if you happen to have “break when exception is thrown” option (Debug->Excptions) turned on. EF is trying to connect to the database but it fails since database does not exist yet so an exception is thrown. You see the exception due to the option above turned on. This exception is actually caught by the Entity Framework and EF will then connect to master database to create the database it was trying to connect in the first step. Once the database is created it will again use the original connection string to talk to the database. So, you can either press F5 to continue debugging since the exception is a first chance exception that will be handled or disable turn off “breaking when exception is thrown” which will result in breaking only for exceptions that are not handled rather than all exception that are thrown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.