Unable to find the requested .Net Framework Data Provider. It may not be installed - sql

I've got an application running on ASP.NET MVC 3 with Entity Framework Code First. In development I was using a SQL Compact database, however upon moving this to my virtual server, I am attempting to target SQL Express.
There were initially issues to do with a "CREATE DATABASE in master" error, which I got around by extracting the model from the SQL Compact database into an SQL script and executing that on the server to create the DB.
I have created a new connection string to point at the SQL Express instance, which uses the EF format:
<add name="LouiseClarkEntities" connectionString="metadata=res://*/Models.LouiseClark.csdl|res://*/Models.LouiseClark.ssdl|res://*/Models.LouiseClark.msl;provider=System.Data.EntityClient;provider connection string="Data Source=.\SQLEXPRESS; Initial Catalog=LouiseClark; User ID=<username>; Password=<password>"" providerName="System.Data.EntityClient" />
The error I am now getting when navigating to a page that uses the DB, is:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
I have installed Entity Framework 4.1 on the server to try and see if this would solve the issue, but it didn't seem to do much good.
Snippet from the stack trace on error page:
[ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.]
System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName) +1420567
System.Data.EntityClient.EntityConnection.GetFactory(String providerString) +35
[ArgumentException: The specified store provider cannot be found in the configuration, or is not valid.]
Any help would be appreciated, as this has been bugging me for days now!
Thanks,
Chris

Use the normal connection string with code first.
<add name="LouiseClarkEntities" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=LouiseClark; User ID=<username>; Password=<password>" providerName="System.Data.SqlClient" />

On your second problem, and partly the cause of the first too...
what you did wrong is which I got around by extracting the model from the SQL Compact database into an SQL script and executing that on the server to create the DB.
You should use migration scripts for that - and use in PM console then Update-Database -Script to dump out what you need - then deploy that to the server Database.
Problem is that there are CF has its own table and data that needs to be initialized properly. If that doesn't match you'll end up with something like that.

Related

In MVC Migration, I can't update-database from one solution but can do it from another

I created a new ASP.NET web application in VS2017 as blank and installed Entity Framework in it and all important libraries.
Problem is:
I successfully ran Enable-Migrations
I successfully ran Add-Migration Initialize
But I can't execute update-database because I can't connect to SQL Server Express just from this project
I tried to open other project and it fork very fine
I think there is a problem in the web.config file because I start from scratch
I add a connection string as follows:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Server=OJ-PC\OJ_SQLEXPRESS;Database=OrdersDb;User Id=sa;Password=******;"
providerName="System.Data.SqlClient" />
</connectionStrings>
I tried a lot of connection strings. I get connection string from SQL Server and add it exactly. I tried same connection string that worked in other project but it didn't work in this project.
In two days I tried everything
Thanks for help!
The . (dot) alias is used when you deal with SQL Server Professional edition.
When it comes to dealing with SQL Express the server name consists of 2 parts separated by backslash \.
For instance
DESKTOP-3PQ45JH\SQLEXPRESS
In this case DESKTOP-3PQ45JH is the name of computer, SQLEXPRESS is the name of server instance.

Error when updating DB: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified

I'm using EF6 and have been making a few changes to my model.
Now all the sudden I get the following error when trying to update my database through the nuget command Update-Database:
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: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
I did not change anything in my connection settings (or web.config in general) but here's my connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\LM.DataAccess.mdf;Initial Catalog=LM.DataAccess;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
In Server Explorer -> Data Connections I can access the database without any problems.
What could be the issue here? I tried deleting the database through the SQL Server Object Explorer but I still get the error.
Still need help people.
Would really appreciate if someone could help me out - I've tried everything I could!
Thats caused when your migrations project is not the StartUp project, which means you'll need to set the connectionstring in the StartUp project for it to work correctly.
Hope it helps.
Best Regards.
I had the same problem, In my case the connection string is being read from the web project not the EntityFramework project, so i made the my Web project the Solution StartUp project so the Package Console Manager can read the connection string, and in Package Console Manager i choosed EntityFramework project as Default project.
Follow step by step:
1./ Create right connect to database in web.config
1./ Right click -> select "Set as default project"
2./ run update-database againt
That's fix.
An error like this can happen if you have multiple projects in the solution. Run update-database - Verbose and ensure that the default project in the package manager console and the StartUp project in the solution explorer are set to the project which contains the code migrations.

Page does not load after I migrated the database from SQL CE to SQL Server

I am running into an issue. I have developed a site in webmatrix with SQL server compact edition. My site is ready and I can see it run without any issue in my PC. However, I ran into an issue after migrating the database to SQL server. Migration finished successfully and I can see all my tables, data in the migrated database. But I can not run the page anymore. In IE browser, it gives me error Page cannot be displayed. (In other browsers it just tries to load indefinitely).
I don't know what is the issue. If I remove the migrated database file(.mdf) and put the .sdf back in APP_DATA folder and connect it in database workspace, my site starts working again.
Note: I can see a connection string added to web.config file when migration is completed.
Verify that you can connect to the migrated database from within SQL Server Management Studio(SSMS). Your connection string in the web.config file needs to be correct and should look something like:
<connectionStrings>
<add connectionString="Server=SERVER01;Database=mydatabase;Uid=user;Pwd=password" name="somename" providerName="System.Data.SqlClient" />
</connectionStrings>
After logging in through SSMS you will know what the settings should be. I hope this helps you.
Thomas, thanks for your reply. Glad to let you know that issue has been resolved. The issue was regarding the sql server version. Basically I had SQL commands like "fetch first n rows only" etc. Now, this is not supported in SQL 2008. My migrated database was in 2008. When I imported the data into SQL 2012 database, it started working now.

Entity framework work locally but not on azure

I have a web project which works perfectly locally.
But when I change the connection string in my published web site on Azure to connect to my database on SQL Azure it will start giving this error.
System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
at MyClass.OnModelCreating(DbModelBuilder modelBuilder) in c:\a\src\MyProject\Model.Context.cs:line 25
at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)
My Config has:
<connectionStrings>
<add name="MyDBEntities" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string="Server=tcp:[Removed].database.windows.net,1433;Database=MyDB;User ID=[Removed];Password=[Removed];Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"" providerName="System.Data.EntityClient" />
<add name="MyDB" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string="Server=tcp:[Removed].database.windows.net,1433;Database=MyDB;User ID=[Removed];Password=[Removed];Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"" providerName="System.Data.EntityClient" />
</connectionStrings>
I tested using my unit test locally with that connection string and it works from my local machine connecting to SQL Azure database.
Any help appreciated.
I was having this exact problem today; it's my first time deploying to Azure. I've been pulling my hair out, except I don't have any left. I finally figured it out, and it's probably the same issue original poster here is having.
Just like the original poster, I tested in these configurations:
ran WCF Web App from Visual Studio against local DB -- success
deployed WCF Web App from Visual Studio to local IIS, ran against local DB -- success
ran WCF Web App from Visual Studio against Azure SQL DB -- success
deployed WCF App to Azure via Visual Studio, running against Azure SQL DB -- FAILURE!!
After reading another post (Code First vs. Database First) I got a hint. That post says that if "connection string has the metadata, EF thinks it is Model First or Database First" but if it's a "plain connection string, EF thinks it is Code First." I browsed the deployed Azure Web Site's web.config and confirmed that the connection string had the proper references to the Model-First metadata. So what was the problem???
I figured that perhaps the Azure Website wasn't reading the web.config's connection string. Thinking back to how I'd created the Azure Web Site, I remembered that I'd given the Azure SQL DB an alias with the exact same name as my connection string's 'label' in the web.config!! To clarify:
in the Azure admin console I went to the Website settings and reviewed the "connection string" settings "baked in" to my Azure web site as a side-effect of creating-website-with-DB -- connection string 'handle' was "SsnCustInfoModelContainer" -- I'd mistakenly given the connection the same 'handle'/'alias' as my web.config 'handle' for the connection string, thinking this would help. Instead, when EF looks for the connection string, it was finding this 'aliased' handle, which was a "plain" SQL connection string containing no metadata. This 'alias' masked the real connection string specified in the web.config.
So I destroyed my Azure SQL DB and my Azure Web Site. Then I recreated the Azure Web Site, but this time I asked for the connection string 'alias' of "SsnCustInfoModelContainer_Proto" for the connection to the associated Azure SQL Server. After initializing the Azure SQL DB from my local SQL Server Management Studio, I deployed the WCF web app again to the Azure Web Site (I had to download a new deployment profile, of course, to do this), I tried the app again. This time it worked -- the 'alias' "SsnCustInfoModelContainer_Proto" did not conflict with and was not found by EF. EF instead went on to find the true connection string, with all the proper metadata, in the web.config. Problem solved.

Using Web Deploy (msdeploy) to publish a WebMatrix site

I started building my site in WebMatrix and then switched to using VS2010 so I could have better Intellisense and debugging. I've been loading WebMatrix to deploy and it's been working fine.
However, loading WebMatrix is a PITA and I actually want more flexibility over the web deployment process.
So I started learning about msdeploy.exe and how to use it. I was able to successfully get the site to sync as I wanted with the following command line:
msdeploy.exe
-verb:sync
-dest:iisApp=MySite,wmsvc=www.mysite.com,username=administrator,password=blahblahblah
-allowUntrusted
-skip:absolutePath=webdeploy.cmd
-skip:absolutePath=web.config
-skip:objectName=dirPath,absolutePath="App_Data"
-skip:objectName=dirPath,absolutePath="bin"
-skip:absolutePath=vwd.webinfo
-source:iisApp="C:\Users\charlie\Documents\Visual Studio 2010\WebSites\MySite"
I had to use -allowTrusted because the cert on the server uses a differnt host name than www. No biggie. I have some -skips for stuff I don't want to write to the dest as well.
It all works great.
I use SQL Server (Express) on my host (a WebMatrix AMI on AWS).
I want to have the ability to push my database to the host as well. I am trying to use the following msdeploy commmand:
msdeploy.exe
-verb:sync
-source:dbFullSql="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=C:....\MySite.mdf;User instance=true"
-dest:dbFullSql="Server=www.mysite.com\SQLEXPRESS;Initial Catalog=webmatrix_db;Uid=webmatrix_user;Pwd=<pwd>"
This gives me
Error: The database 'webmatrix_db' could not be created.
Error: A network-related or instance-specific error occurred while establishing aa
connection to SQL Server. The server was not found or was not accessible. ...
I think my problem is the connection string. I copied Server=".\SQLEXPRESS;Initial Catalog=webmatrix_db;Uid=webmatrix_user;Pwd=<pwd> from the WebMatrix UI and pre-pended it with www.mysite.com thinking it needed my hostname somewhere.
Obviously this is not correct and I can't find any examples of connection strings that work either.
Note that SQL is not exposed directly by this server. I assume WebMatrix's invocation of msdeploy is connecting using my admin credentials (not the SQL credentials) first and then msdeploy invokes the SQL commands on the remote host. I need something like the ...wmsvc=www.mysite.com,username=administrator,password=blahblahblah in the -dest option of the first example I gave above.
It would be awesome if I could see a log of how WebMatrix was invoking msdeploy.
What is the correct msdeploy command to do what I want?
[UPDATE - ANSWER]
One of the best things about StackOverflow, is that posting a question really makes you think about what you are doing. Shortly after I posted the above, I realized the wmsvc=www.mysite.com,username=administrator,password=blahblahblah parameter in the -dest parameter was the key. The question became how to correctly add it to my specific example.
This msdeploy command line now connects correctly:
msdeploy.exe -verb:sync -source:dbFullSql="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=C:\Users\charlie\Documents\Visual Studio 2010\WebSites\Fiinom\App_Data\MySite.mdf;User instance=true" -dest:dbFullSql="Server=.\SQLEXPRESS;Initial Catalog=webmatrix_db;Uid=webmatrix_user; Pwd=rI2vP3rK6hV8nN8",wmsvc=www.mysite.com,username=administrator,password=blahblahblah -allowUntrusted
Now that msdeploy is connecting successfully and executing commands, I need to figure out how to make it actually merge the database. Right now it's giving me an error that a table already exists and can't create it...
This is related to your side comment on merging the database...
Currently Web Deploy does not have any provider that supports database merges - the dbFullSql provider uses SQL Server Management Objects ("SMO") to script out the db contents and we then apply it on the other side. Effectively Web Deploy thus will only overwrite the destination db with the source db.
If you are okay with that as a "merge" you can get around that table already exists error by using SMO scripting options - this is what WebMatrix does to make the db publishing/downloading work. To your source just add:
,scriptDropsFirst=true
(this scripts out drops for all the objects in your source database so that if they exist on the destination they will get dropped and won't block you)
You might also need:
,copyAllUsers=false
(if you aren't a sysadmin on the remote SQL database, chances are you won't be able to create logins, which are a server-level action. Typically if you don't use this setting, you'll get an error about creating a login, or login doesn't exist, because SMO scripts our your database user as "for LOGIN " and that login doesn't exist on the server)
Hope that helps!
Kristina