Unable to find the requested .Net Framework Data Provider. (SqlClient) - c#

I am trying to set up a simple ASP.NET MVC 4 webapp using DB first migrations from a SQL Server (2005). I have created the tables in the database and used Entity Framework to create the objects in the code. I can access the data using these objects.
The problems come when I try to initialize the WebSecurity using WebSecurity.InitializeDatabaseConnection("FLMREntities", "UserProfile", "UserId", "UserName", true); in the Global.asax.cs file. I have tried using the InitializeSimpleMembershipAttribute filter that came with the template and got the same issue. I get the error message:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
Here is the relevant connection string:
<add name="FLMREntities"
connectionString="metadata=res://*/Models.FLMR.csdl|res://*/Models.FLMR.ssdl|res://*/Models.FLMR.msl;
provider=System.Data.SqlClient;
provider connection string="data source=notes.marietta.edu;
initial catalog=muskwater;
user id=muskwater;password=********;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
Also, I have created the membership tables in the database to match what the template creates. If I change the final parameter in the Initialize call to false (so that it does not try to create the tables automatically) it returns that it cannot find the UserProfile table. I have also tried variations on the names, such as [dbo].[UserProfile].
All I need is to have a simple account model to allow users to log in and allow certain users to see more content.

I had a similar problem, what I've done:
I modified the default connection. I already had a connection string for the edmx model, which looked like this:
<add name="ChemicalReporterEntities" connectionString="metadata=res://*/ChemicalDB.csdl|res://*/ChemicalDB.ssdl|res://*/ChemicalDB.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLExpress;initial catalog=ChemicalReporter;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
But I cannot used it with the SimpleMemebrship provider. So in Visual Studio I opened the Server Explorer > Data Connections, I selected my connection, right click, properties and I copied the connection string from there and pasted it to defaultConnection:
<add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;Initial Catalog=ChemicalReporter;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" />
After that I changed WebSecurity.InitializeDatabaseConnection to use the DefaultConnection.
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", true);

This could be a duplicate of this.
From what I remember about this problem, you need to make sure that the sqldata provider is registered. Sometimes, in your machine.config there is a duplicate entry that you need to delete and if I remember correctly, there could be an erroneous entry that you need to remove. Have a look at this msdn post, the info for this is about halfway down the page.
The link to the other SO has some links that could be helpful. If this ends up not being marked as a duplicate question, I can add them here as well or whatever is the proper thing to do.
The section you are looking for will look similar to this I think:
<system.data>
<DbProviderFactories>
<add name="SqlClient Data Provider"
invariant="System.Data.SqlClient"
description=".Net Framework Data Provider for SqlServer"
type="System.Data.SqlClient.SqlClientFactory, System.Data,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
</DbProviderFactories>
</system.data>
From the information you have provided in comments, it seems that this is missing in your machine.config. Unless I am mistaken (possible) that means that the SQL Provider isn't registered on your machine. You'll need to add the above section to your web.config. You'll also need to make sure that System.Data.SqlClient is referenced in your project.
If this isn't installed on your target machine, you'll have more work to do, getting it installed. I wish I could help you with that more, but when I did this, it was for SqlServerCE, so the files I used are much different.
Good luck!
EDIT:
A critical piece of information, is the version needs to match the file that you are using. in type="blabh..blah..blah..Version=2.0.0.0, blah blah" that needs to be the version of the file you are using so right click your file and get the file version. If it fails, try variations of the version. I believe my file was 2.0.0.1 but Version=2.0 is what worked when I did it. Just try a few different versions based on your version number (2.0, 2.0.0, 2.0.0.1 for example).

Just wanted to chime in here, that I had this issue, by looking at my machine.config for the targeted .NET version that I was running off of... within the structure:
ensure that your targeted data providers are correctly inputted within there, only one per provider (I have seen duplicates cause problems), as well as, ensure that it is valid XML, and you only have one DBProviderFactories entry. In my case, I had a entry after the initial entry (essentially two entries, one with my providers, one blank), and the blank was was causing all the issues.
Hope this helps someone with the same issue :)

Related

Changing connection string after moving database to App_Data folder?

I am trying to learn mvc.net. I created a small project in which I used Model first approach. The problem is I wanted my database to be shown in App_data folder for that I followed this article:
http://www.dotnetfunda.com/articles/show/2182/how-to-embed-sql-database-in-appdata-folder
In short I detached the database and then I include the database in app_data folder. Now I am facing problem in changing the connection string, Previously I generated connection string automatically using entity framework, It was like:
<add name="KeepitrememberEntities"
connectionString="metadata=res://*/EDM.csdl|res://*/EDM.ssdl|res://*/EDM.msl;
provider=System.Data.SqlClient;provider connection string="
data source=mypcname\SQLEXPRESS;initial catalog=Keepitremember;
integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
and now I changed it to:
<add name="KeepitrememberEntities"
connectionString="Data Source=mypcname\SQLEXPRESS;
AttachDbFilename=|DataDirectory|Keepitremember.mdf;Integrated Security=True;
User Instance=True"
providerName="System.Data.SqlClient" />
When I am executing the code and trying to save the value using a form, it's showing me the error in EDM.Context.cs and error is:
An exception of type
'System.Data.Entity.Infrastructure.UnintentionalCodeFirstException'
occurred in Emptymvc.dll but was not handled in user code
Additional information: 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.
What I need to do now, any solution!!
Thanks for your time.
As far as I see, the first connection string is Entity Framework connection string, and the second one is SQL Express connection string. In an EF project, both of them should be exist in web.config file for DbContext and code generation template usage - hence they're not interchangeable (i.e. you should not change EF connection string into SQL Express one and vice versa).
Since you have changed EF connection string contained CSDL, SSDL & MSL information required by EF into SQL Express one, EF assumes the existing database metadata isn't exist anymore and trying to create a new database like Code First has, hence it triggers UnintentionalCodeFirstException after executing OnModelCreating method.
Instead of changing EF connection string in web.config, just add SQL Express connection string on the same connectionStrings element as this (you may need to define initial catalog name when required):
<connectionStrings>
<!-- SQL Express connection string -->
<add name="KeepitrememberConnection"
connectionString="Data Source=mypcname\SQLEXPRESS;Initial Catalog=Keepitremember;
AttachDbFilename=|DataDirectory|Keepitremember.mdf;Integrated Security=True;
User Instance=True"
providerName="System.Data.SqlClient" />
<!-- EF connection string -->
<add name="KeepitrememberEntities"
connectionString="metadata=res://*/EDM.csdl|res://*/EDM.ssdl|res://*/EDM.msl;
provider=System.Data.SqlClient;provider connection string="
data source=mypcname\SQLEXPRESS;initial catalog=Keepitremember;
integrated security=True;MultipleActiveResultSets=True App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
NB: The connection string names should be same with predefined names in EF model generation schema.
Related issues:
Model First with DbContext, Fails to initialize new DataBase
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode
Entity Framework cant use the DbContext, model being created

Code-First Migrations for multiple databases?

I have the following connection string:
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="MyContext" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string='data source=SQLSERVERDB;initial catalog=TestDB_CodeFirst;user id=***;password=***;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
When I try to enable migrations I first get a warning:
Cannot determine a valid start-up project. Using project 'MyApp.Model' instead.
Your configuration file and working directory may not be set as expected.
Use the -StartUpProjectName parameter to set one explicitly.
Then I get this exception:
Argument 'xmlReader' is not valid. A minimum of one .ssdl artifact must be supplied.
Is the connection string wrong and why should I need ssdl if I'm using Code First?
NOTE
My context is in MyApp.Model project where my Migrations folder should be located.
I don't have connection strings in my main startup project because connection strings are retrieved from a second database and the user can select one of them when logging in to the application.
I have just one connection string shown above in my MyApp.Model project which points to my development database.
Also, my second question is:
If I use CF migrations, will all databases be migrated each time a user selects a different database for the first time?
EDIT
I changed the connection as mentioned below, and I get the following exception:
The item with identity 'table1' already exists in the metadata collection.
Parameter name: item
It must be noted that I reverse-engineered an existing database. So I don't know what possibly went wrong!
I've also deleted the Migrations folder and checked the database but there is no migration_history table created.
You are trying to use a connectionString designed to work with Database First / Model First. You can tell because your providerName is System.Data.EntityClient instead of System.Data.SqlClient.
Your connection string should look like this:
<connectionStrings>
<add name="MyContext"
connectionString="Data Source=SQLSERVERDB; Initial Catalog=TestDB_CodeFirst;user id=***;password=***;"
providerName="System.Data.SqlClient" />
</connectionStrings
Although I would suggest using Integrated Security instead of a user/password. Just personal preference, though.

Where are the logins in my MVC C # program?

Has made the following guide: http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part1-cs
It automatically creates options for users to register and login. Where are they stored? Can not find it in the database. How do I check if someone is logged in?
It stores the information in a SQL Compact 4.0 database.
By default the ASP.NET MVC X Web Application project template uses the ASP.NET Membership Provider to store and manage the users which according to the article are stored in a SQL Compact 4.0 database. But if I recall correctly there isn't an officially supported membership provider for this database.
Sure you could write your own or use a third party solution such as:
http://sqlcemembership.codeplex.com/
However, if I download the code for the guide and check out the Web.config I notice that the SqlMembershipProvider is used and this is linked to the connection string called "ApplicationServices".
<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="ApplicationServices"
... />
Let's take a look at that connection string:
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;
User Instance=true"
providerName="System.Data.SqlClient" />
Apparently its stored in a local database file called "aspnetdb.mdf" which is probably located in your web application's App_Data folder.
That is where you can find the user data. But is it actually used in this guide? Appears not, might just be a remnant of the default ASP.NET MVC Web Application project template.
The movie-related data is stored in an SQL Compact 4.0 database. A different connection string is used for it.
<add name="MovieDBContext"
connectionString="Data Source=|DataDirectory|Movies.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
Also a local database file, but this time it is a different provider (SqlServerCe.4.0. instead of SqlClient).

Using EF Code First and SqlCe in WPF

I'm trying to use EF Code First with my WPF application , the idea is to create a SqlCe Db in AppData/MyApp (if there isn't one) and use it with EF Code First.
At the moment it's giving error when I try to read data off the database it supposed to create , but when I checked the db context object I saw it's trying to create it in SqlExpress.
First of all , how can I set it to work with CE instead of SqlExpress and set the file location?
I tried changing the connection string in app.config but couldn't get it to work (it didn't created sdf file) and also I'm not sure how to set connection string path to AppData folder as it's in User folder (not fixed).
Never worked with SqlCe or EF Code First before , so any help is welcome & appreciated.
Thanks in advance.
I've managed to get it working after a lot of messing around. My app.config has the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Database"
connectionString="Data Source=Database.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0"
invariant="System.Data.SqlServerCe.4.0"
description=".NET Framework Data Provider for Microsoft SQL Server Compact"
type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
</DbProviderFactories>
</system.data>
</configuration>
Having the DbProviderFactory in there will help when it comes to deployment too. It will allow users to use SQLCE 4 without running the installer for it (providing you supply the native binares as well, some info here: http://erikej.blogspot.com/2011/02/using-sql-server-compact-40-with.html).
Here is a possible clue. If you were to install EFCodeFirst.SqlServerCompact using NuGet Package manager, and look in: Visual Studio 2010\Projects\MyProject\packages\EFCodeFirst.SqlServerCompact.0.8.8482\Content, it does things for your web app like:
public static class AppStart_SQLCEEntityFramework {
public static void Start() {
DbDatabase.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
// Sets the default database initialization code for working with Sql Server Compact databases
// Uncomment this line and replace CONTEXT_NAME with the name of your DbContext if you are
// using your DbContext to create and manage your database
//DbDatabase.SetInitializer(new CreateCeDatabaseIfNotExists<CONTEXT_NAME>());
}
}
I don't think the tooling is ready for SQLCE for WPF projects. It stopped me in my tracks too.
This probably isn't going to help in the short run, but I think it would be the best solution in the long term. I haven't been able to find where anyone else has documented how to do this so I guess I'll have to figure it out as I go unless someone with more intimate knowledge of using SqlCE4, CodeFirst, and creating NuGet packages beats me to it. What we need is a NuGet package, either a new package or make the existing one smart enough to handle it, that can add SqlCE4 and CodeFirst to a WPF and/or WinForms project.
The existing package seems geared toward web projects since it depends on WebActivator. From what I can tell, installing WebActivator on a non-web project really isn't appropriate. I don't know if it's possible for a NuGet package to detect the project type and execute different installation logic based on that information. It would be better all around for whoever is responsible for maintaining the EFCodeFirst.SqlServerCompact package to either release a non-web version or make the existing one smarter.
Pending that, in my spare time I'm going to see if I can figure out how to do it and create my own. I can't promise how long it'll take me because I'm almost totally in the dark with regards to creating NuGet packages. I've created a couple of really basic ones but have never done any advanced stuff like config transforms and the like.

C# Configuration Manager . ConnectionStrings

I have a console app containing an application configuration file containing one connection string as shown below:
<configuration>
<connectionStrings>
<add name="Target"
connectionString="server=MYSERVER; Database=MYDB; Integrated Security=SSPI;" />
</connectionStrings>
</configuration>
When I pass this to my Connection using:
ConfigurationManager.ConnectionStrings[1].ToString()
I have two values in there, hence using the second in the collection, my question is where is this second coming from?
I have checked the \Bin version and original and its not mine! Its obviously a system generated one but I have not seen this before? Can anyone enlighten me?
The mystery connection string is:
data source=.\SQLEXPRESS;
Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;
User Instance=true
This isn't a problem as such I would just like to know why this is occuring? Thanks in advance!
For future reference to those who may or may not stumble on this, after discovering the machine.config, it's become apparent it is bad practice to refer to a config by its index as each stack will potentially be different, which is why "Keys" are used.
In this instance my code would be:
ConfigurationManager.ConnectionStrings["Target"].ToString()
Cheers all!
Check your machine.config. If you only want your entry, you can add a <clear /> element to the <connectionStrings> element like so ...
<connectionStrings>
<clear />
<add name="Target"
connectionString=
"server=MYSERVER; Database=MYDB; Integrated Security=SSPI;" />
</connectionStrings>
Check your machine.config (under WindowsDir\Framework). I just checked mine and I have the same thing.
It's defined in the machine.config file, and it's global to all .Net applications on the machine.
Connection strings "stack" through the configuration hierarchy, which is why the element name for your own connection string is tagged "add". You're adding your own connection string to the list of machine-level connection strings.
It might be preferable to use:
ConfigurationManager.ConnectionStrings["Target"].ConnectionString
to ensure you get your own connection string, even if the machine.config is modified.
The Index first Element in the Connection Strings tag is stored By default And it's represents the > MemberShip database Connection String.
Cause of the Consideration for CLR is to be used during your application , So that the CLR On your behalf attaches it By default .
Like what is the CLR Doing with Default Constructor in the Class Creation without Constructor.

Categories

Resources