Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login failed for user '' and Cannot open database "Database1.mdf" requested by the login. The login failed. Login failed for user 'rBcollo-PC\rBcollo'

So..I managed to almost finish all my problems.But now i deal with another one. I used this connectionstring :

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Database=Database1.mdf");

And it throws the next error:

Login failed for user '

If i remove the .mdf extension it throws the same error

Now if i'll add the following to the string:

Integrated Security = true

It throws this:

Cannot open database "Database1.mdf" requested by the login. The login failed. Login failed for user 'rBcollo-PC\rBcollo'.

P.S for Integrated Security = false it throws the "Login failed for user'"

The problem is that i'm not using any username or password for the Database.

Any help,please?

like image 326
Dabuleanu Catalin Avatar asked May 11 '16 12:05

Dabuleanu Catalin


1 Answers

Note: My answer would be applicable if you are using EntityFramework Core with SQL Server

This error could be because of the reason 'Database does not exist' and application try to perform DB operations.

All you need to use the following line before performing any database operation.

_context.Database.EnsureCreated(); 

And what is EnsureCreated?

    // Summary:
    //     Ensures that the database for the context exists. If it exists, no action is
    //     taken. If it does not exist then the database and all its schema are created.
    //     If the database exists, then no effort is made to ensure it is compatible with
    //     the model for this context.
    //     Note that this API does not use migrations to create the database. In addition,
    //     the database that is created cannot be later updated using migrations. If you
    //     are targeting a relational database and using migrations, you can use the DbContext.Database.Migrate()
    //     method to ensure the database is created and all migrations are applied.
    //
    // Returns:
    //     True if the database is created, false if it already existed.
like image 199
immirza Avatar answered Sep 27 '22 22:09

immirza