Could not resolve host '(localdb)' on Visual Studio for MacOS - sql

Developing an Asp.Net web Api working with SQL. I'm stuck at debugging app on my Mac. When calling methods, working with db, app returns instead this error:
System.Net.Sockets.SocketException
Could not resolve host '(localdb)'
Feels like a mistake is somewhere in this line (according to other answers): Web.config file
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(localdb)\MSSQLLocalDB; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|MyDatabaseContext-12.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
This app works perfectly fine in VS for Windows. Any ideas?
Update: I have an sql server in azure, so I have a connection string. Any way to make it used instead of local?

Related

Unable to find the requested .Net Framework Data Provider.

I've seen several posts about this, but none of the answers seem to work.
I'm trying to set up simplemembership and I keep getting this error when WebSecurity.InitializeDatabaseConnection(...) is called
Here is the ConnectionsStrings entry:
<add name="JBarterMain" connectionString="Data Source=winsrv01;Initial Catalog=ProvausioJBarter;Persist Security Info=True;User ID=xxx;Password=xxx" providerName="System.Data.SqlClient"/>
I tried adding the following to my machine config in the .net 4.0 x64 folder:
<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" />
I'm getting absolutely nowhere. This seems incredibly odd to me that this wouldn't work out of the box. I had another project a few months ago (broken currently so I can't verify that it still works) that was working just fine and I didn't have to do any additional configuration.
Any ideas?
I wrote a tutorial on how to set up simple membership Here. Also I prefer to ditch the filters and initialize it directly on application start

MVC4 - Membership - SQL Server Compact 4.0 - Exception: Unable to find the requested .Net Framework Data Provider

The InitializeDatabaseConnection Method documentation says we can use the MVC 4 "Internet" template's generated membership functionality with SQLCE (SQL Server Compact 4.0 Local Database).
I've used this template many times before and have never had a problem when I didn't fiddle with anything and simply used the localDb (the default) that is generated by the Entity Framework the first time we register a user.
My requirement is to get the membership tables that are by default generated into 2012 localDb database instead to generate into a SQLCE database. However, I'm getting the following:
Exception: Unable to find the requested .Net Framework Data Provider. It may not be installed.
To do this we simply:
Open Visual Studio (express works fine).
Generate a new MVC4 --> Internet (with account) project.
Add a SQLCE database to the ~/App_Data/ folder (right-click the folder and select Add --> SQL Server Compact 4.0 Local Database).
Add table then a record to the table.
Right-click the Models folder and select Add --> ADO.NET Entity Data Model
Open the (root) web.config and copy the name of the 'connectionstring'(<add name="ceEntities")
Locate the following line of code in the ~/Filters/InitializeSimpleMembershipAttribute class: WebSecurity.InitializeDatabaseConnection("ceEntities", "UserProfile", "UserId", "UserName", autoCreateTables: true);
F5 to compile/run/debug the project
Once the home/index page loads click the "Register" link in the upper right hand corner
Enter a username and password to register and click 'Ok.'
Up to this point the code compiles and runs fine however when the following line of code is run in the ~/Filters/InitializeSimpleMembershipAttribute class:
WebSecurity.InitializeDatabaseConnection("ceEntities", "UserProfile", "UserId", "UserName", autoCreateTables: true);
This exception is caught:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
Which is apparently fixed when we add this code to the web.config:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<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>
And when we run it again the following exception is caught:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
Thank you to Scott Hanselman and whoever made him aware of the Membership Provider with SQLCE protocol as in his Introducing System.Web.Providers - ASP.NET Universal Providers for Session, Membership, Roles and User Profile on SQL Compact and SQL Azure blog post Scott outlines the steps to implement the ASP.NET Universal Providers. This helped me as he outlined the proper connection string outline for SQLCE:
<connectionstrings>
<add name="Sql_CE"
connectionstring="Data Source=|DataDirectory|\MyWebSite.sdf;"
providername="System.Data.SqlServerCe.4.0">
</add>
</connectionstrings>
So I updated my connectionstring to:
<connectionstrings>
<add name="defaultconnection"
connectionstring="Data Source=|DataDirectory|\ce.sdf;"
providername="System.Data.SqlServerCe.4.0">
</add>
</connectionstrings>
Then updated connectionStringName the ~/Filters/InitializeSimpleMembershipAttribute class back to:
WebSecurity.InitializeDatabaseConnection("defaultconnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
The application compiled without an error or exception so I ran the application then Registered a user and in that process the applicable SimpleMembership tables were generated into my "ce.sdf" (SQL Server Compact 4.0 Local Database).
I am now able to utilize the SimpleMembership default's at their fullest!
p.s., For brevity I'm going to now edit my initial post/question to omit the stacktraces, leaving only the exceptions.
Random suggestion but it may need to be added.as a namespace to the web.config file or as a package in the packages file, I could be wrong

ASP.NET MVC4 Simple Membership fails to initialize if using Entity Connection String (works with SQL connection string)

I am developing ASP.NET MVC4 application with Entity Framework 5, and I am using Model First mode.
However Simple Membership will throw the following error if I am using the connection string generated by the designer:
......
InnerException: System.InvalidOperationException
HResult=-2146233079
Message=The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588
Source=STD
StackTrace:
at SistemTempahanDewan.Filters.InitializeSimpleMembershipAttribute.SimpleMembershipInitializer..ctor() in c:\Users\Orang\Documents\Visual Studio 2012\Projects\STD\STD\Filters\InitializeSimpleMembershipAttribute.cs:line 45
InnerException: System.ArgumentException
HResult=-2147024809
Message=Unable to find the requested .Net Framework Data Provider. It may not be installed.
Source=System.Data
StackTrace:
at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
at WebMatrix.Data.DbProviderFactoryWrapper.CreateConnection(String connectionString)
....
Connection string generated by the designer (will not work):
<add name="STD" connectionString="metadata=res://*/Models.STD.csdl|res://*/Models.STD.ssdl|res://*/Models.STD.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=STD;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Connection string I add manually (will work):
<add name="STD" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=STD;Integrated Security=True;MultipleActiveResultSets=True" />
Yes, SimpleMembership only works with SQL Server connection strings, so you need such an entry in your web.config. Not ideal, but it works as you say...
This may be your answer: Using SimpleMembership with EF model-first
Jon Galloway's article is an excellent resource of information about SimpleMembership, if you haven't already seen it: http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx

Connecting to an existing database in a ASP.NET MVC4 Web API project

I'm building an web api using ASP.NET web api framework. I have seen a few tutorials on how to get started but I haven't found to use an existing SQL Server database. I have the database up and running also added the data connection to the database explorer in VS but I don't know how to connect the database to my project so I can start using it as my repository. How can I do that?
Put the following in your application web.config file and replace "yourConnectionString" with the actual connection string.
<configuration>
<connectionStrings>
<add name="DbConnection" connectionString="yourConnectionString" />
</connectionStrings>
</configuration>
In the code use System.Configuration.ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString to get its value. You might need to reference the "System.Configuration" assembly in your project.

Deployment of encrypted app.config

I've created a console .Net application and encrypted the app.config file with RSAProtectedConfigurationProvider. I've used an article http://www.codeproject.com/KB/dotnet/EncryptingTheAppConfig.aspx as an example for the Encryption. It works fine in an IDE on my XP desktop.
Then I deployed my application to a Windows 2003 server. The package included the app's executable along with the encrypted app.exe.config file. However, the application failed on the server with the following exception:
Unhandled Exception: System.Configuration.ConfigurationErrorsException: Failed to decrypt using provider 'RsaProtectedConfigurationProvider'. Error message from the provider: The RSA key container could not be opened.
I decided to encrypt app.config on the server so I copied app.config and encrypted it. The app.exe.config looked encrypted and I ran the application. However, it failed: although it didn't raise an Rsa exception as before, it couldn't read a connection string. After I decrypted app.exe.config, I found that it contained only the below string
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
although the original app.config had contained an application-specific connection string.
I think that in order to resolve my issue I should export the RSA keys from my desktop to the server. Can you advise how can I do it (it's not a Web application!).
Many thanks for your advice.
I've found a solution in the article APP.config encryption with RSA and Deployment.
It is suggested to use custom keys. They are created, exported and imported with the aspnet_regiis utility.
Here are my notes:
It works fine if IIS is installed on the development desktop and the target server. But for my Console application IIS is not needed,
and it may not exist on the server. The author of the article provides a workaround in this case. I've found it a little
complicated and used aspnet_regiis because IIS is installed both on my source and target machines.
After adding the configProtectedData section to my app.config file, Visual Studio created warnings:
The 'keyContainerName' attribute is not declared.
The 'useMachineContainer' attribute is not declared.
The 'description' attribute is not declared.
As per the article Visual Studio throws warnings when sections of Web.Config are encrypted using RSA by Darth Zar, the warnings can be ignored for Web.config.
I've ignored them in my case (for app.config), and everything worked fine.
I hope my post has been useful.