Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Using Entity Framework 'Generate DB from Model' from WPF app and avoid 'login failed' during db.SaveChanges()

$
0
0

My original (and still pending) problem is how to get my desktop WPF application to successfully open and update a MS SQL database. The exception and trace symptoms are:

"The underlying provider failed on Open"

"The login failed.\r\nLogin failed for user PHILB\\Phillip"

Error 18456, severity 14, state 38

Thanks to several posts, I was able to get this far.

I am using Visual Studio 2012, the 'about' says it has SQL Server data tools 11.1.

-        Using Tools / Options / Database Tools / Data Connections, the SQL Server Instance is (LocalDB)\v11.0

My MS SQL Server Management Studio is for SQL Server 2012, version 11.0.

My C# desktop application is a simple WPF project using Entity Framework for 'model first'. So I create one simple table, save all changes, right click the entity design window to:

  • 'Generate Database from Model'
  • click the Previous Button so the Generate Database wizard appears
  • click on New Connection

-       in Connections Properties window click on Advanced button

  • in AttachDbFilename I put:            C:\Users\....\Stats2011H.mdf
  • the Data Source shows:                 (LocalDB)\v11.0
  • click OK to close the Advanced Properties window

-       back in the Connections Properties window

  • leave 'Use Windows Authentication' selected
  • click OK

-       Pop-up says do you want to create a new DB?

  • click yes

-       Eventually the DDL Script appears and I click 'Finish'

-       Before running the DDL script I check

  • the App.Config has been updated for the DB like:

data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Model\Data\Stats2011J.mdf

  • in Visual Studio, an empty DB has been created – good
  • in SQL Server Management Studio, the DB is there – good
  • in Visual Studio / Server Explorer the DB is there – good

-       Then I run the DDL script

  • connect using:                   (LocalDb)\V11.0
  • set connecting / options / tab for additional connection parameters to: AttachDbFilename=C:\Users\xxxx \Model\Data\Stats2011H.mdf; database=Stats2011H;
  • script runs with no errors
  • disconnect from the server

-       Check the DB in SQL Server Management Studio

  • DB is there with the one table – good

-       In Visual Studio I update the app.config file to:

  • replace 'attachDBfilename=' with 'database=|DataDirectory|\Model\Data\Stats2011J.mdf
  • per reading a post, this fix helped me get past the error for " A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

-       Then I run my desktop WPF program which adds items to the one table

-       then the program does:                 inDBConnection.SaveChanges();

-       this throws an exception showing:

"The underlying provider failed on Open"

The inner exception showed:

"The login failed.\r\nLogin failed for user PHILB\\Phillip"

-       sorry I did not include the stack, and now I cannot recreate this test due to further problems below

  • the top of the stack regarded SQLClient.SQLInternalConnectionTds

-       the above user name seems correct – that is me, the only user of my laptop with administrative privileges.

-       the SQL Server trace additionally showed:

  • Error 18456, severity 14, state 38

-       after reading many logs about this problem, I checked this DB via SQL Server Management Studio

  • the DB was setup for both Windows and SQL Server authentication

-       Q1: why can't my desktop program logon to the server and use this database?

-       Since I had done these tests a lot, I thought I would delete some of the old DB's.

  • in SQL Server Management Studio, I deleted some DB's including the latest Stats2011H.mdf per above.
  • I then deleted that database from my Visual Studio project so I could try again (as I had done before)
  • Perhaps this order of deleting was a problem? In the past I may have deleted from Visual Studio first.

-       now I cannot even open SQL Server Management Studio, it keeps reporting the latest error, about how the login fails for this nowdeleted database:

-       Q2: How can I get SQL Server Management Studio to open?

  • the database it cannot open has been deleted (at least I told it to delete)

-       In Visual Studio:

  • Server Explorer shows some older DB's, but not 'Stats2011H'
  • SQL Server Object Explorer shows no user databases under the (localdb)\Projects for SQL Server 11.0, only the folder for 'System Databases'.

-       I have closed all apps and restarted Windows – no help.

Any help is very appreciated, thank you in advance.



philb222


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>