Connection string error using membership provider from WCF service - asp.net

The target is: I need a wcf method which will create user in the database.
I am using membership provider. and My client is SmartPhone/mobile device.
I wrote a method which will create user BUT when I test the method I get the following error.
error:
An error occurred while attempting to initialize a System.Data.SqlClient.SqlConnection object. The value that was provided for the connection string may be wrong, or it may contain an invalid syntax.
Parameter name: connectionString
And I get this error in the following line:
Membership.CreateUser(name, password,email,"question","answer",true,out createStatus);
I am using the same connectionString that I use to read data from the database.
My connection string is:
<add name="MajangoEntities" connectionString="metadata=res://*/EntityModel.MajangoEntityModel.csdl|res://*/EntityModel.MajangoEntityModel.ssdl|res://*/EntityModel.MajangoEntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:sql2k298.discountasp.net;initial catalog=SQL2008R2_835021_myddb;user id=SQL2008R2_835021_mydb_user;password=mypassword;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Can anybody help me with some ideas??

That connection string includes some Entity Framework specific data. Try instead just the "provider connection string" embedded within it:
<add name="MajangoEntities" connectionString="data source=tcp:sql2k298.discountasp.net;initial catalog=SQL2008R2_835021_myddb;user id=SQL2008R2_835021_mydb_user;password=mypassword;multipleactiveresultsets=True;App=EntityFramework">
The rest of the data in there (the metadata, etc.) is just confusing SQL Server.

Related

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

I am having problem in running my ASP.Net MVC project which is an admin project. It connects with a sql server database to fetch record. When I try to build and run, I am welcomed with this error: "Unable to find requested .Net Framework Data Provider. It may not be installed".
Here is the link to this error.
I have tried many solutions available on stackoverflow but none of them worked for me that's why I am posting this question here again. I tried changing the Target Framework version in Properties of project to all the available versions but all in vain. Here is the screenshot of my Global.asax.cs file which is causing this exception.
Below is the connection string of my project:
<add name="AlkhaleejEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Alkhaleej;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>
Connection string provided above indicates EF connection provider being used:
connectionString="metadata=res:///Models.Model1.csdl|res:///Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Alkhaleej;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"
Note that SimpleMembershipProvider can't use EF provider connection string (see this post) due to usage of SqlClient provider instead of EntityClient, hence it requires SQL Server connection string to interact with database.
Try open Server Explorer => Data Connections => right click your server connection, select Properties => add provided SQL Server connection string something like this one (leave EF connection string part as is, just add SqlClient provider connection string):
<add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=Alkhaleej;Integrated Security=True" providerName="System.Data.SqlClient" />
Then, change connectionStringName parameter on WebSecurity.InitializeDatabaseConnection method inside SimpleMembershipInitializer class to SQL Server connection string name as given below:
public class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
// other code part
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "Email", autoCreateTables: true);
}
}
}
NB: The ideal setup for EF projects should include at least one SqlClient connection string and one EntityClient connection string, which may serve different providers with same target database.
Similar issues:
SimpleMembershipInitializer won't initialize
Unable to find the requested .Net Framework Data Provider. (SqlClient)
Can SimpleMembership and Entity Framework share a connection string?

Setting EF Connection String in Azure Web App

We have an ASP .NET (MVC) app and are using Entity Framework 6 to connect to our databases. The DbContext is constructed in a standard way and it loads the connection string on our behalf. The generated code looks like this:
public partial class MyContext : DbContext
{
public MyContext(string connectionName)
: base("name=" + connectionName)
{
}
}
We set the connection string in a local web.config also in a standard way:
<configuration>
<connectionStrings>
<add name="DefaultConnection"
connectionString="metadata=...;provider connection string="...""
providerName="System.Data.EntityClient" />
When we publish the app to Azure we navigate to the Azure Portal, then to the Web App's Settings, then to the list of Connection Strings. There we add the EF connection string that we had used locally. When we restart and visit the app we get a run-time error depending on the type of connection string we choose.
For a Custom type we get the following run-time error:
Keyword not supported: 'data source'.
For SQL Server or SQL Database we get the following run-time error:
Keyword not supported: 'metadata'.
This really seems like a straightforward story so we are wondering what is going wrong.
The problem is the escaped quotes: ".
The connection strings in web.config have quotes escaped because they are serialized in an XML attribute. When entering a connection string in the Azure portal you should provide the raw unescaped string. Something like this:
metadata=...;provider connection string="Data Source=..."
David Ebbo's answer is good for confirming that the Environment is set up as you expect. It is also helpful to pay attention to the .pubxml file when publishing via the wizard in Visual Studio: it will try to populate connection strings as well.
'custom' should be correct here. In that case, the providerName is left unchanged, so if you have System.Data.EntityClient in your config, that should remain after the Azure runtime changes it.
Try going to Kudu Console and click on Environment to make sure the conn string looks correct there.
If you have this line in web.connfig
<add name="Entities" connectionString="metadata=res://*/TestDB.csdl|res://*/TestDB.ssdl|res://*/TestDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=XXXXXXXX.database.windows.net,1433;Initial Catalog=YourDB;User ID=YourUser;Password=XXXXXX"" providerName="System.Data.EntityClient" />
Add this in azure portal:
Name Column => Entities
Value Column => metadata=res://*/TestDB.csdl|res://*/TestDB.ssdl|res://*/TestDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=XXXXXXXX.database.windows.net,1433;Initial Catalog=YourDB;User ID=YourUser;Password=XXXXXX"
"Custom" - In the drop selection box
Make sure (as stated in first answer) to replace " with "
Just recording my own experiences in addition to answers already recorded here
This was my final connection string (on mutliple lines for clarity)
metadata=res://*/Models.mBT.csdl|res://*/Models.mBT.ssdl|res://*/Models.mBT.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=tcp:myazureserver.database.windows.net,1433;
Initial Catalog=databasename;
User ID=z#myazureserver;
Password=xyz"
To convert from a "normal" connection string to one that is accepted by EF:
The connection string type in the application settings has to be "other" not "SQL Azure"
The connection string value automatically replaces anything published in web.config
The metadata name Models.mBT.csdl (and the other two) comes from this:
Firstly, mBT is the name of my .edmx file
With regards to the Model. bit, see the answer from #leqid here: MetadataException: Unable to load the specified metadata resource
You can inspect your obj directory and see these three metadata files are in a subfolder called Models, so you need to prepend with Models.

The entity type is not part of the model for the current context

My ASP.NET application is working locally, but when I deploy it, I get the following error: "The entity type Usuario is not part of the model for the current context". I've tried every solution I found, but I couldn't get it work.
I think it can be something wrong in connection string because they are different ones to local database and remote database. This is the connection string in Web.Release.config file:
<add name="ModelContainer" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source = tcp:ebmzszqsw8.database.windows.net,1433; Initial Catalog = {database_name}; User ID = {my_id}; Password={my_password};Trusted_Connection=False;Encrypt=True;Connection Timeout=30"" providerName="System.Data.EntityClient" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />

connectionstring to sqlmembershipprovider asp.net

I have added the sqlmembershipprovider to my dynamic data project for logging in and registering and such.
However, I am having problems with the connection string. The database is on a remote sql server 2008 database server.
Below I have a connection string for the entities:
<add name="xxEntities"
connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string="Data Source=xx.xx.x.xxx;Initial Catalog=XX;Integrated Security=True;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
When I hit the membership provider, I get this error:
Keyword not supported: 'metadata'.
on this line of code:
System.Web.Security.Membership.GetAllUsers(0, 1, count)
Do I need a separate connection string for the provider? Or should I be able to use the above string with all my connections, including my membership provider?
thanks.
Try using this connectionString template + change the User-ID, Password, Initial Catalog
Provider=SQLOLEDB.1;Persist Security Info=True;Data Source=xxx.xxx.xxx.xxx;User ID=RemoteApp;Password=xxx;Initial Catalog=xxx;

ASP.NET MVC : Using the same database for Entity Framework and ASP.NET Membership

Im trying to user ASP.NET MVC3 with Entity Framework and ASP.NET Membership for authentication. I've set up an existing database as my application services database for membership.
When i create the entity data model through the wizard it adds the following connection string to my web.config.
<add name="DBEntities" connectionString="metadata=res://*/Models.DB.csdl|res://*/Models.DB.ssdl|res://*/Models.DB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=PM\SQLEXPRESS;Initial Catalog=DB;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
I'm a noob so i don't really understand why but i tried using the same connection string for my membership provider and failed (probably because of this: providerName="System.Data.EntityClient" ?).
So i added a separate connection string to the same database and used it for the membership provider.
<add name="ApplicationServices" connectionString="Data Source=PM\SQLEXPRESS;Initial Catalog=DB;Integrated Security=True" providerName="System.Data.SqlClient"/>
It works fine when i use ASP.NET configuration to add user and etc. But when i run the application and try to do something like validating a user i get an error;
A transport-level error has occurred when receiving results from the server. (provider: Shared Memory Provider, error: 0 - The handle is invalid.)
I made a guess this has something to do with my connection strings. I'm hoping some of you experts can help. Cheers.
Have you tried restarting the server : A transport-level error has occurred when receiving results from the server ?
Not a solution or an answer, just a pointer to a similar issue..

Resources