Create new console application and paste the code below:
public class Configuration : DbMigrationsConfiguration<MyContext>
{
}
public class MyContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
}
public class Contact
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var context = new MyContext())
{
Console.WriteLine("Contacts: {0}", context.Contacts.Count());
}
}
}
Running the above ends with the exception below. Deleting the Configuration class makes it work.
Unhandled Exception: System.Data.Entity.Core.EntityException: The underlying pro
vider failed on Open. ---> System.Data.SqlClient.SqlException: Cannot open datab
ase "ConsoleApplication2.MyContext" requested by the login. The login failed.
Login failed for user 'domain\user'.
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow
ningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean o
nlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& co
nnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow
ningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbCon
nectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection
owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions
, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1
retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass
1.<Execute>b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](
Func`1 func)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action ac
tion)
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
--- End of inner exception stack trace ---
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func
`1 func, Boolean throwOnExistingTransaction, Boolean startLocalTransaction, Bool
ean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResul
ts>b__9()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](
Func`1 func)
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMer
geOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.
IEnumerable<T>.GetEnumerator>b__0()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunct
ion>b__3[TResult](IEnumerable`1 sequence)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TR
esult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQue
ryProvider.Execute[TResult](Expression expression)
at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expressi
on expression)
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
at ConsoleApplication2.Program.Main(String[] args) in Program
.cs:line 57
public class Configuration : DbMigrationsConfiguration<MyContext>
{
}
public class MyContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
}
public class Contact
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var context = new MyContext())
{
Console.WriteLine("Contacts: {0}", context.Contacts.Count());
}
}
}
Running the above ends with the exception below. Deleting the Configuration class makes it work.
Unhandled Exception: System.Data.Entity.Core.EntityException: The underlying pro
vider failed on Open. ---> System.Data.SqlClient.SqlException: Cannot open datab
ase "ConsoleApplication2.MyContext" requested by the login. The login failed.
Login failed for user 'domain\user'.
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow
ningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean o
nlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& co
nnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow
ningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbCon
nectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection
owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions
, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1
retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass
1.<Execute>b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](
Func`1 func)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action ac
tion)
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
--- End of inner exception stack trace ---
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func
`1 func, Boolean throwOnExistingTransaction, Boolean startLocalTransaction, Bool
ean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResul
ts>b__9()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](
Func`1 func)
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMer
geOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.
IEnumerable<T>.GetEnumerator>b__0()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunct
ion>b__3[TResult](IEnumerable`1 sequence)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TR
esult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQue
ryProvider.Execute[TResult](Expression expression)
at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expressi
on expression)
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
at ConsoleApplication2.Program.Main(String[] args) in Program
.cs:line 57