I used EF 4.1 ( Code First ) in the MVC 3 project a while ago, and that was good.
Today I tried to use EF 4.3.1 ( Code First ) in a WinForms project and ran into some real voodoo: (The original project I was working on was WinForms, however the same is true for the attached code for the Console application.)
When I try to enter a simple class into the database, I get the following exception:
Unable to open the Test database requested by login. Login failed. Login failed for user "[ComputerName]] [[Administrator]".
I tried checking SQL Server configurations (2008 R2), but my user has all permissions on the server.
NOTE. The database does not exist the first time the application is launched.
Even the following code example does not work:
class Program
{
public static void Main()
{
var person = new Person { Name = "Nathan" };
using (var db = new MyContext())
{
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 tried reinstalling EF 4.3.1 - to no avail. Restarting SQL-Server also had no effect.
NOTE . I noticed quite a lot of similar questions, but none of them coincided with mine and answered - here I hope that I am lucky here :)
To clarify: The access that is denied to the user is the Owner (local administrator) of the machine.
EDIT: I did some experiments and I found something rather strange:
"# 1" db.Database.Exists() "", : " , " , , .

, False.
2:
- . .
- db.Database.Create() . , , , SQL Management Studio.
, ? !
db.Database.CreateIfNotExists();, .
db.Database.Initialize(true); ...
: , , , , : P , , , , DropCreateIfModelChanges DropCreateAlways Initalizer ( ).
.
[ , Entity Framework, SQL Server.]