I seem to be having a problem with Entity Framework code-first. I managed to make it create the database in my project folder in App_data and everything works well if I do not try to check the contents of the generated database in the Server Explorer.
If I do that and try to open the application I get this error:
Cannot open database “SellCars” requested by the login. The login failed.
Login failed for user ‘Aly-PC\Aly’.
While this behavior may be normal (not really sure), even if I detach the db and close the connection in the server explorer and I run this application it still does not work but instead in throws this error:
One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup.
Cannot open database “SellCars” requested by the login. The login failed.
Login failed for user ‘Aly-PC\Aly’.
Log file ‘D:\Projects IDE\Visual Studio\MyWork\Websites\SellCars\SellCars\App_Data\SellCars.ldf’ does not match the primary file. It may be from a different database or the log may have been rebuilt previously
This is my connection string:
<add name="CarsEntities"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='D:\Projects IDE\Visual Studio\MyWork\Websites\SellCars\SellCars\App_Data\SellCars.mdf';Initial Catalog=SellCars;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
I do not try to change anything inside of the database not even add any data but I still get these errors.
What should I do?
Add
Aly-PC\Alyto the active directory users who can access the SQL server. Then add the SQL server login you just created to the database.