2

I'm trying to create a database using Entity Framework with a code-first approach, but it fails on creating the database with this exception:

Unhandled Exception: System.Data.DataException:
An exception occurred while initializing the database. See the InnerException for details.

System.Data.Entity.Core.EntityException: The underlying provider failed on Open.

System.Data.SqlClient.SqlException: Cannot open database "X.Xcontext" requested by the login. The login failed. Login failed for user 'This-PC\ME'.

Any idea how to resolve this ?

Gamal Mohamed
  • 149
  • 1
  • 12
  • As per exception, I think that SQL has not enabled for windows login or your connection string is not properly build with integrated security. Kindly cross check both scenario. – Mohd Ismail Siddiqui May 01 '17 at 14:01
  • It looks like the user This-PC\ME doesn't have permissions to this database called X.Xcontext. You'll have to set up this user with permissions before they can create tables and so forth. – criticalfix May 01 '17 at 14:01
  • @criticalfix How can I set up user with permissions in VS ? – Gamal Mohamed May 01 '17 at 14:08
  • Use SQL Server Management Studio. You may need a database administrator to do this part if you're not familiar with SSMS, or if you don't have access on the server. – criticalfix May 01 '17 at 14:11
  • Possible duplicate of [Cannot open database "test" requested by the login. The login failed. Login failed for user 'xyz\ASPNET'](http://stackoverflow.com/questions/2575907/cannot-open-database-test-requested-by-the-login-the-login-failed-login-fail) – Steve Greene May 01 '17 at 15:23
  • @SteveGreene No it isn't! It's addressing the problem in Database first approach not code first as mine.. – Gamal Mohamed May 01 '17 at 16:13

2 Answers2

0

Your user does not have permissions to create a database, you will need to contact your DBA for permissions, or depending on where you work you'll need to create a ticket to have someone create the database for you.

RH6
  • 144
  • 8
  • I'm working on my local machine, how can I change users permissions ? – Gamal Mohamed May 01 '17 at 14:44
  • Try this https://esha.zendesk.com/hc/en-us/articles/202977089-Granting-Access-to-Additional-Users-with-SQL-Server-Management-Studio – RH6 May 01 '17 at 14:48
0

Well, Problem solved when I just renamed my DbContext..Don't know why,but it worked for me!

Gamal Mohamed
  • 149
  • 1
  • 12