Connection string for remote membership aspnet.mdf - asp.net-membership

I have an MVC4 application with an aspnet.mdf database with membership tables and I’ve added two more tables to it. Locally I’m using “AccountModels.cs” to handle membership and a Database first EDMX modal to handle the two other tables. So I have two connection strings that are working great locally.
Remotely, my Database first EDMX modal "aspnetEntities" connection string works fine but I don’t know what I need to do to get the "DefaultConnection" connection string to work, this is what I had hoped would work:
Remote DefaultConnection not working
<add name="DefaultConnection" connectionString="Data Source=mydatabase.db.3730456.hostedresource.com; Initial Catalog=mydatabase; Persist Security Info=True; MultipleActiveResultSets=True; User ID=mydatabase; Password=mypass" providerName="System.Data.SqlClient"/>
Locally it looks like this, but it's generated for me when I add the EDMX
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
And my working remote aspnetEntities connection string
<add name="aspnetEntities" connectionString="metadata=res://*/Models.RegisterModel.csdl|res://*/Models.RegisterModel.ssdl|res://*/Models.RegisterModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=mydatabase.db.3730456.hostedresource.com; Initial Catalog=mydatabase; User ID=mydatabase; Password='mypass'"" providerName="System.Data.EntityClient" />
When I try to run the membership part of the site remotely, I get an error saying it’s trying to build a new database,
CREATE DATABASE permission denied in database 'master'.
Can someone help me out with a connection string that will work?

Never mind, the answer is to not use the default membership provider and build your own custom one. Aspnet.mdf is way too bloated anyway!

Related

Using the same DbCompiledModel to create contexts against different types of database servers is not supported

I am learning ASP.NET MVC from tutorials of Microsoft :
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/accessing-your-model's-https://stackoverflow.com/editing-helpdata-from-a-controller
At the link above mentioned, while adding a controller named "MoviesController" , i am getting this error
"Unable to retreive metadata for 'MvcMovie.Models.Movie'. Using the same DbCompiledModel to create contexts against different types of database servers is not supported. Instead, create a separate DbCompiledModel for each type of server being used"
How can i fix that?
I had the same issue.
I switched providerName="System.Data.SqlServerCe.4.0" with providerName="System.Data.SqlClient", and it created the Controller and Views.
I found this: http://msdn.microsoft.com/en-US/library/ms171861.aspx
I followed the directions and added a reference to SQL Server Compact, but it still doesn't work.
I also tried commenting out the default SQL Server Express connection, but it still gave the same error when trying to add the controller.
I'm just going to use SQL Server Express. I will let you know if I have any issues.
I've found that using the following works:
(Assuming that you've got SQL Server Express or higher installed)
<add name="MovieDBContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;AttachDBFilename=|DataDirectory|MovieDB.sdf"/>
I think the reason of this error is because the VS is having some difficult to reuse your existing DBContext, while scaffolding. The VS try to use a property in DBContext with similar name of the Domain (like trying the Domain name concatenating "s"). If it cant find the property and your context already has a DBSet with you Domain class, it generates that error.
My solution was to create a new DBContext named "DeleteContext". After creating with success the controller and views, I have replaced the "DeleteContext" in my Controller to my existing one. Finally I deleted the "DeleteContext" class.
It works really fine.
That solution was not working for me, it blows on return View(db.Movies.ToList());
Instead use this :
<add name="MovieDBContext" connectionString="Data Source=|DataDirectory|Movie.sdf" providerName="System.Data.SqlServerCe.4.0"/>
and this:
public class MovieDBContext : DbContext
{
public MovieDBContext() : base("Movie") { }
public DbSet<Movie> Movies { get; set; }
}
I ran into the same error on another Microsoft ASP.NET MVC tutorial while using Visual Studio 2012 (http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-5).
I decided that for the sake of completing the tutorial, it was easier to just use SQL Express in place of SQL CE.
What I did to resolve the issue was delete MvcMusicStore.sdf from Server Explorer, then deleted the same database file from App_Data in Solution Explorer.
I updated the connection strings section in the Web.config to use a Sql Express database (.mdf) in place of Sql CE (.sdf). For this particular tutorial, MusicStoreEntities is the name of the class that extends DbContext:
<connectionStrings>
<add name="MusicStoreEntities" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MvcMusicStore.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
I built the solution, ran the site, and the database was regenerated for me. To add the database file back to the solution, click the "Show All Files" button in Solution Explorer, right-click the .mdf file in App_Data, and select "Include in Project."
Hi there's one solution that worked fine to me. In your Web.config, the tutorial told you to add the following line in the connectionStrings section:
<add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0"/>
Do not delete it or change it!
When you're adding a new controller you can make this to avoid the error that is presenting:
Comment the line above.
Add the following line:
<'add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlClient"/>
Save your solution
Add the controller
Before you run your app, uncomment the line you have commented (1), and comment the line you added (the one with the providerName="System.Data.SqlClient") (2).
This worked very fine to me, when adding the controllers.
Hope this could help you.
I just tryed a bit around and found the problem.
In the tutorial you are adding the following line in your web.config
<add name="MovieDBContext" connectionString="Data Source=|DataDirectory|Movies.sdf" providerName="System.Data.SqlServerCe.4.0"/>
It seems that there is a problem with it.. i just commented it out and used the SqlServer and it works for me. If you still want to use the SqlServerCe you need to take a look how to fix that problem.
If you're using VS 2012, you will need to also tell EF to use SQL Compact instead of localDb.
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
An easier way is to install the EF SQL Compact Nuget package.
For more details, check out this blog entry.
I ran into this same problem while working through the Contoso University asp.net MVC tutorial. It appears that the problem comes from mixing the SQL Server Compact connection strings with the Membership provider Sql Server connection.
I initially used hyperGeoMetric's fix, and that did work. Then I looked at the downloadable code's web.config and noticed some additional configuration.
If you add/replace the default parameter of entityFramework with this:
<parameters>
<parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
add a system.data section like this:
<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 modify the existing (default) DefaultConnection to look like this:
<add name="DefaultConnection" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|aspnet-membership.sdf" />
After these modification, I was able to continue the tutorial using the Sql Server Compact edition.
Sam
I followed that tutorial too, and got the same issue. I deleted the connection string, then I was able to add the controller, and it created the other files. Then I added the same connection string again to the Web.config file, inserted it ecxactly to the same place where it was before. It solved my problem.
I'm learning this tutorial:http://mvcmusicstore.codeplex.com/. and got the same error. I found a item name "EntityFramework.SqlServerCompact" in the NuGet packages list, and install it, all things go fine! note, the packages dependent entityframework, you can view all the version here:https://www.nuget.org/packages/EntityFramework.SqlServerCompact. GOOD LUCK!
the path of data source is wrong , you can add a "\" before "Movie.sdf".
like this:
<add name="MovieDBContext" connectionString="Data Source=|DataDirectory|\Movie.sdf" providerName="System.Data.SqlServerCe.4.0"/>
I ran into the same problem in another solution. It appeared just after I introduced a parametrized call to the base constructor, like the one included in the default UsersContext class.
This fails
public class MovieDBContext : DbContext
{
public MovieDBContext() : base("DefaultConnection")
{
}
...
}
This works
public class MovieDBContext : DbContext
{
// No constructor here
...
}
It seems that naming the connection string in the constructor creates the error. I only have one connection string in my web.config, so DefaultConnection is still used, although I don't name it explicitly.

MVC - Use remote SQL database

How do you change an MVC3 web app to use a remote server for the data context? I tried changing the connection string the in web config (and restarted everything) but it still uses the local SQL database.
Edit: my connection string is below
<add name="ApplicationServices" connectionString="Data Source=zzz.database.windows.net;Initial Catalog=SMS;Persist Security Info=True;User ID=xyz;Password=abc" providerName="System.Data.SqlClient" />
Edit 2: I tried commenting out everything inside the <connectionStrings> tag. The site still works. I'm a bit confused, is the connection set somewhere else?
If you are using Entity Framework code first (which comes with asp.net mvc 3), the name of the database class corresponds with the name of the connection string. For example if you have a class like this:
public class DataContext : System.Data.Entity.DbContext
{
[...]
}
The name of your connection string should also be DataContext. More info here at step 4.
You change the connection string in your web.config. For example if you are using a SQL server your connection string could look like this:
<add name="ApplicationServices"
connectionString="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
providerName="System.Data.SqlClient" />
where Data Source points to the name of the remote SQL server, Initial Catalog the name of the database to connect to and User Id and Password seem pretty self explanatory.
Now obviously in your code you will use the ApplicationServices connection string somewhere, so depending on what data access you are using or whether you are using an ORM there might be different places to look for. So if despite changing the connection string in your web.config your code continues to use a local database, well, it's obvious that this is hardcoded somewhere in your code.

How to set up new Membership and Session providers to run in Windows Azure? Using MVC3 and Web Role

I've read about the New Membership and Session providers, and the information in this article is that just changing the connectionStrings the database would build itself magically in my SQL Azure Database.
So, I first changed my connection to:
<add name="Project" connectionString="Server=tcp:xxxxxxxx.database.windows.net,xxxx;Database=xxxxx;User ID=xxxxxxxxxxx;Password=xxxxxxxxxxx;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.SqlClient" />
When I tried to debug my app with the emulator, I got the error:
EFProviders require MultipleActiveResultSets=True for System.Data.SqlClient connection strings.
I researched about this error and realized that it was something related to Entity Framework. I tried to make an Entity Framework connection string with no success. In addtion, I read somewhere that SQL Azure doesn't support MultipleActiveResultSets.
So, I have 2 questions:
Is it true that if I run the application and register any user via website interface the Membership and Session tables, views and procedures will magically build in my SQL Azure database?
What am I missing to make my app run?
Detail: I didn't add any new item in Models folder. It's only AccountModels class, as it generates from MVC3 template.
Fortunatelly now I have the 2 answers:
Yes, it's true and very easy!
The mistake I did first time it was try to put MultipleActiveResultsSet as a new attribute in the connectionString line. The code have to be like this:
<add name="Project" connectionString="Server=tcp:xxxxxxxx.database.windows.net,xxxx;Database=xxxxx;User ID=xxxxxxxxxxx;Password=xxxxxxxxxxx;Trusted_Connection=False;Encrypt=True;**MultipleActiveResultSets=True**" providerName="System.Data.SqlClient" />
Edited
Now I found out other way to do that with AppFabric Caching for Session State. Just follow this tutorial.

mvc3 code-first error on sqlexpress

When using sqlexpress
<add name="SchoolContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=CatomMvc;Integrated Security=True"
providerName="System.Data.SqlClient" />
I get an error
Model compatibility cannot be checked because the database does not
contain model metadata. Ensure that IncludeMetadataConvention has been
added to the DbModelBuilder conventions.
When I change to
<add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0"/>
It's work but I want to use sqlexpress or sql 2008 that gave the same error.
The database CatomMvc may already exist in .\sqlexpress.
Try to rename the database to something like Catomvc_backup or drop the database, then try again.
Alternatively you could use the following within your Application_Start():
Database.SetInitializer(new DropCreateDatabaseAlways<YourContext>());
This will force the Database to be recreated, but generally during development you should only use:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<YourContext>());
To allow the Database to be recreated based on changes to your model.
However, neither of these should make it into your production code!
Just make sure your Database.SetInitializer function is at the top of your Application_Start function.
Does the trick :)
Sometime it happens because you dont have sysadmin role on local SQL Server instance. I found useful article where you can download script command and install it. You will be ask for SQLEXPRESS name or instance name, just you can write 'SQLEXPRESS' and enter. Thats it. It will add you to sysadmin role of local server instance.
Download command from: http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=addselftosqlsysadmin&DownloadId=9198
Please let me know if it does not work for you.

Oracle Session State Store Class in ASP.Net C#

I am implementing in a development project Oracle Session State Store Class, which is found in Oracle 11g Client to hold my users Sessions as the application is being run over a web farm.
Whilst I can make Oracle Session State Store work through the web.config, I need to make the store work in the code behind because our database connections i.e. the passwords are encrypted and are only decrypted when a connection is made. Unfortunately, I cannot do this in the web.config.
I have tried to implement the Oracle Session State Store Class in the Global.asax in the Session_Start method using the Initialise method. The problem is that it is throwing an error. The exception it is calling is "The connectionStringName attribute is empty or does not exist in the configuration file, or an invalid attribute is found in the configuration file."
In the Session_Start method, I created a Name Value Collection and added two variables, one of which is the database connection string and the other the type for the custom session store.
Does anyone know what I am doing wrong?
Thanks
First, make sure your web.config or machine.config is configured correctly. You can take a look at Oracle Session State Store documentation, or below:
<?xml version="1.0"?>
<configuration xmlns=
"http://schemas.microsoft.com/.NetConfiguration/v2.0">
<connectionStrings>
<add name="my_sessionstate_app_con_string" connectionString=
"User Id=scott;Password=tiger;Data Source=Oracle"/>
</connectionStrings>
<system.web>
<!-- Enable and customize OracleSessionStateProvider -->
<sessionState mode="Custom" customProvider="MyOracleSessionStateStore">
<providers>
<add name="MyOracleSessionStateStore"
type="Oracle.Web.SessionState.OracleSessionStateStore,
Oracle.Web, Version=2.111.6.20, Culture=neutral,
PublicKeyToken=89b483f429c47342"
connectionStringName="my_sessionstate_app_con_string"/>
</providers>
</sessionState>
</system.web>
</configuration>
Second, you don't HAVE to encrypt your Oracle session state store connection string during development time. You can use aspnet_regiis to encrypt it at deployment like below.
aspnet_regiis -pef connectionStrings "c:\inetpub\wwwroot\myapp\"
If the command line above runs fine but your web app produces RSA error messages, try to add the following accounts (or the specific ones you use) to the RSA container:
aspnet_regiis -pc "NetFrameworkConfigurationKey" -exp
aspnet_regiis -pa "NetFrameworkConfigurationKey" "ASPNET"
aspnet_regiis -pa "NetFrameworkConfigurationKey" "NETWORK SERVICE"
aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE"
By the way, I didn't have to implement OracleSessionStateStore in Global.ascx. I just reference with Session["key"] = value; and var value = Session["key"];
Also, make sure your DBA setup a job to clean up the session state table.

Resources