EF Code first after deployment throws ProviderIncompatibleException - sql-server

I have a simple EF 5.0 / Code First application developed in VS2012. In development it uses SQL Server CE. However, when I deployed it on a test server, I am getting the following stack of errors:
ProviderIncompatibleException: An error occurred while getting provider information from the database.
This can be caused by Entity Framework using an incorrect connection string.
Check the inner exceptions for details and ensure that the connection string is correct.
ProviderIncompatibleException: The provider did not return a ProviderManifestToken string
SqlException (0x80131904): Login failed for user 'MYDOMAIN\MYSERVER$'
IIS and the database are on the same server (MYSERVER). It's running Win2012, (with IIS 8 and .NET 4.5) and SQL Server 2012.
There are two things in web.config that caught my attention. First, connection string. I generated it from IIS UI; and I used my old connection strings that had "Data Source", rather than server. I used localhost and server name. I created a db user. I read somewhere that the database doesn't exist - so I deleted it. I gave permissions to NT AUTHORITY\NT SERVICE in the database. I probably did some other permutations - but the result is always the same.
The second place was Connection Factory under entity framework section. Apparently, it wasn't changed by Web Deploy; type was still SqlCeConnectionFactory, and parameter was System.Data.SqlServerCe.4.0. I saw somewhere that SQL Server was the default - so I deleted it; and when that didn't work, I changed the values to SqlConnectionFactory and System.Data.SqlServer correspondingly - but again, result is the same.
By now I ran out of ideas... Given that I have a problem even when I am reading the data from existing database - I am missing something simple...

Related

Calling EnsureDbCreated in Entity Framework Core throws exception "Login failed for user Dev1"

I'm using EF Core in a small test application and I'm trying to create the database in SQL Server 2014 Express. Initially I used Update-Database script to create it and I didn't have any issues but when I tried to do this at runtime with dbContext.Database.EnsureCreated(), it no longer worked. I have checked the credentials for the SQL user multiple times and I'm sure that both approaches use the same. If I use the Update-Database script first, then all the queries work successfully in runtime using the same credentials.
Any idea what I might be missing here?

Connecting to sql database

(I am a sql noob and I just can not figure this out on my own)
For some time now I have been trying to establish a connection to a SQL database in codename one but to no avail. First I tried connecting to a MariaDB database from one.com. All that's needed for the connection is
Database db = Display.getInstance().openOrCreate("databaseName");
if I am not mistaken, but I am guessing this implies that I have somehow already established a connection to the database. This is not the case however so it creates a new .sql file, right? I can recall that you can connect to a database in the services tab in Netbeans. I chose the MySQL(Connector/ J Driver) which should work with MariaDB, or should it? I entered all my data and i says that it can not establish connection to the database.
the error i get
So I thought I might as well try using localhost. I used XAMPP to host a database and connected in the netbeans services tab.
connected?
Now testing was needed to see if this works. I started the SQL journey with this https://www.codenameone.com/manual/files-storage-networking.html#_sql and integrated the part after "You can probably integrate this code into your app as a debugging tool". I changed database name to "mybase" (it's existance can be confirmed in picture 2). Ran the app, opened the dialog, entered "select ID from customers" and got: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: customers) It does not get past the first call to "executeQuery". The customers table definitely exists so what am I missing to establish connection?
I really need instructions to connect to the localhost database and ideally also to the one hosted by my webhost provider.
Thanks,
Jona
The Database class is to access the SQLite DB on the mobile device. To connect to external databases, you'd have to do something different, such as a ConnectionRequest or Socket I think.

Entity Framework Initializer throws A network-related or instance-specific error on one machine

We discovered very strange behavior with Entity Framework last night. It was probably the result of my misguided approach to code first against an existing database, I solved it by turning off the initializer.
Database.SetInitializer<DataContext>(null);
But I would still like to know how such behavior was possible and if it's something we need to be worried about for future EF deployments. Maybe EF code first is not stable across all environments.
We have an existing database that supports the functionality of an existing legacy app. I added new functionality to the app with "code first" and ended up needing to share the same database. So we created the new table with a SQL script and turned on EF Migrations. This all worked fine for me locally, and also worked against the SQL Server instance on my colleague's machine.
However when he tried to run it all from his machine, against the same database, the EF call always timed out with the error:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: Named
Pipes Provider, error: 40 - Could not open a connection to SQL Server
We even hard coded a different connection string to see if it would build its own separate database. Same error.
We finally figured out it was the Initializer because while inspecting the DbContext at a break point in the debugger, we triggered the initializer. If we paused execution and expanded the DbContext in a watch window, the database would initialize and the process would continue without error!
It's also worth noting that things were working on his machine until EF noticed model changes. That's when we enabled migrations and updated the table via script and permanently broke the Initializer on his machine.
This is one of the most bizarre things I've experienced. What could cause the EF initializer to hang and throw false network errors about an otherwise functional database...but then work from the debugger? And only on one machine!

The underlying provider failed on ConnectionString (|DataDirectory| and LocalDB)

I'm getting the exception
The underlying provider failed on ConnectionString.
with inner exception
Invalid value for key 'attachdbfilename'
on a windows 7 home premium virtual machine as i'm trying to deploy and debug my application.
However, on my development machine, the application just launches without problems.
the connection string is :
<add name="AssetsLocalDBEntities" connectionString="metadata=res://*/AssetsLocalDB.csdl|res://*/AssetsLocalDB.ssdl|res://*/AssetsLocalDB.msl;provider=System.Data.SqlClient;provider connection string="data source=(localdb)\v11.0;attachdbfilename=|DataDirectory|\AssetDatabase.mdf;initial catalog=AssetDBSource;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Steve Wellens has mentioned that the connection string contains garbage in this topic:
The underlying provider failed on ConnectionString
yet, as Maxim Gueivandov has replied, this should valid connection string, as it's directly generated by the EntityFramework's edmx designer.
At least, i believe it to be valid, as it does work on the development machine.
Also in another topic (Entity Framework - The underlying provider failed on ConnectionString), the accepted answer is to remove the " and replace them by \", as the string is a literal in the c# code, yet i still have my connection string in xml, so " cannot simply be replaced.
on-connectionstring
The .NET version installed on the virtual machine is 4.0.30319 and MsSQL Server Express LocalDB is 11.0.2318.0, so i believe they are both up to date.
Edit: If i remove "|DataDirectory|" from the connection string i get the following error on my development machine:
Cannot attach the file 'AssetDatabase.mdf' as database 'AssetDBSource'.
So i also remove "initial catalog=AssetDBSource" and i get
An attempt to attach an auto-named database for file AssetDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
i can emagine this is because it doesn't know where to look? so, i provide it the full path (C:..\AssetDatabase.mdf) and it works.
So the remainder of the question is: how do i get relative paths working with LocalDB?
I believe the target machine doesn't have the Update 4.0.2 or later for .NET 4 installed. The .NET 4 version number can be confusing, as version 4.0.30319 seems to be used by all .NET 4 builds. In effect it does not mean your machine has the Update 4.0.2.
Can you install .NET 4 Update 4.0.3 (latest .NET 4 update as of today) on the target machine and see if the problem goes away?
Or just verify that the update is installed - just build a simple application in .NET 4 that uses SqlClient to connect to LocalDB using the connection string, and record the error information in case you still hit any errors. The EF error message is fairly generic, and I don't know how to get a detailed error from the underlying SqlClient provider.
Alternatively you could also migrate it to .NET 4.5.

The best way to update a table on Windows CE device from Server

I need to modify an existing Windows CE app which gets it data from SQL Compact database (sdf) to update the database table (only 1 table) with latest records from a SQL Server Express when ever it is docked to the machine.
I came across Microsoft Sync Framework and it seems to be more than capable of enabling me to achieve my requirement. I tried one of their Walkthrough: Creating an Occasionally Connected Smart Device Application, but was not able to get the app working (the app I created based on the guide as well the sample code). The WCF service is hosted successfully and I can browse to the service from the browser as well.
I am encountering an error at the below line on the click event of SynchronizeMenuItem.
Dim syncStats As Global.Microsoft.Synchronization.Data.SyncStatistics = syncAgent.Synchronize()
Below is the trace from the output window:
'MobileSyncServices.exe' (Managed): Loaded 'System.SR.dll'
A first chance exception of type 'System.Net.WebException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
The thread 0x771e2c1e has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
I've been trying to find a solution for this problem as I didn't come across any after 2 days hence posting it here. I've installed all the framework/ service pack which are prerequisite for this as well. Have any of you tried this walk through and encountered a similar issue? What baffles me further is, that the sample code is also failing in two of my development machines (1 is Win XP and the Win 7).
Is there any other alternative way to update table in a Windows CE device with SQL Express Server when docked?
The requirment was to find a method to overwrite an table on a docked device (connected through ActiveSync/ Sync Center), achieved this by connecting to the SQL Server directly (by specifying either ip address) from the Pocket PC.
User would specify the SQL Server credentials Under system config/ options, which would be made use of to connect to the SQL Server. Once the desired data from the Server is broght down, the compact database is updated with the relevant data.
Below web resources paved the way to solve this issue and achieve the goal.
Connecting to SQL Server Express from a Pocket PC application
Accessing SQL Server Express from the emulator (or PDA)

Resources