MVC Entity framework from local database to SQL database Azure - sql

Possibly a very simple question, but it has already taken me several days. The problem:
In my project, MVC Entity framework, and on my local computer it connects with a local database and that works as expected. I can add items to the database and remove its etc. At azure, I have a SQL database and when I deploy the project to azure I get the following error message
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: 52 -
Unable to locate a Local Database Runtime installation. Verify that SQL
Server Express is properly installed and that the Local Database Runtime
feature is enabled.)]
I ques that the my connection string is incorrectly and my application can't connect to the azure SQL database. I tried to add and change the connection string, but somehow I can't make it work. This is what I have (minus password and username) :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings><add name="InventoryEntities" connectionString="metadata=res://*/Administration.csdl|res://*/Administration.ssdl|res://*/Administration.msl;provider=System.Data.SqlClient;provider connection string="Server=tcp:****.database.windows.net,1433;Initial Catalog=****;Persist Security Info=False;User ID=****;Password=****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"" providerName="System.Data.EntityClient" /></connectionStrings>
</configuration>
What am I doing wrong?

After a night sleep, I finally got it working.First of all, I had to make clear which database I was going to be use where the DbContext was initialized
public class EFDbContext : DbContext
{
public EFDbContext() : base("name=InventoryEntities")
{
}
}
With InventoryEntities being the name of the connectionstring in the web.config of the WebUI which I did not change.
Simple as that, but it took days......

Related

Entity Framework, Custom Control, and No connection string named x, Still Runs

I recently rewrote a large program that my company uses to conduct business. I changed to Entity Framework early on. For the most part it has been working quite well.
One issue that has annoyed me for quite some time and now I have time to look into is, a custom control that pulls a question from the database causes a strange error.
When the custom control used SQL it would actually show the question on form display in the designer. At first this was pretty cool and I liked it. After switching it to use Entity Framework 6, when I load the form in the designer I get the error
No connection string named 'wotcDB' could be found in the application
config file.
It's totally in the app.config file. In fact the program has no problem compiling and running without issue. For the longest time I would just push Ignore and go to work on the form. The question of course no longer shows up in the designer.
The User Control is part of the application. The app.config file is there, and the program both runs and deploys fine. What's causing this?
Here is my app.config, ####### is sensitive data.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="webDB" connectionString="metadata=res://*/Data.webDB.csdl|res://*/Data.webDB.ssdl|res://*/Data.webDB.msl;provider=MySql.Data.MySqlClient;provider connection string="user id=#######;password=#######;server=#######;database=#######;persistsecurityinfo=True""
providerName="System.Data.EntityClient" />
<add name="wotcDB" connectionString="metadata=res://*/Data.wotcDB.csdl|res://*/Data.wotcDB.ssdl|res://*/Data.wotcDB.msl;provider=System.Data.SqlClient;provider connection string="data source=#######;initial catalog=#######;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider></providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data></configuration>
Rather than deal with the issue, since only I see it. I've found an answer at
This Stack Article
I put
If (System.ComponentModel.LicenseManager.UsageMode = System.ComponentModel.LicenseUsageMode.Designtime) Then
Load_Question()
End If
in the custom control load. I don't see the question during design time, which is fine for me. My other option was to put the connection string into the EF class, which is difficult to keep up since I'm using EF-Design and not code first.

how to handle database reallocation for dotnetnuke website

i have a website that used to run on sqlexpress data base i changed my server and it stopped running i ran it using visual studi and IIS but it eith gives me error 404 or error 503 i think the problem is in my webconfig sql string connection her is my code:
<connectionStrings>
<add name="SiteSqlServer" connectionString="Data Source=localhost;Initial Catalog=ULTRA; User ID=admin;Password=123" providerName="System.Data.SqlClient" />
<add name="SiteSqlServer" connectionString="Server=(local);Database=DotNetNuke;uid=;pwd=;" providerName="System.Data.SqlClient" />
-->
<!-- Connection String for SQL Server 2008/2012 Express -->
<!--<add name="SiteSqlServer" connectionString="Data Source=localhost;Initial Catalog=ULTRA; User ID=admin;Password=123" providerName="System.Data.SqlClient" />-->
<!-- Connection String for SQL Server 2008/2012
-->
<!--<add name="SiteSqlServer" connectionString="Server=(local);Database=DotNetNuke;uid=;pwd=;" providerName="System.Data.SqlClient" />-->
</connectionStrings>
<appSettings>
<!-- Connection String for SQL Server 2008/2012 Express - kept for backwards compatability - legacy modules -->
<!--<add key="SiteSqlServer" value="Data Source=.\SQLExpress;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True" />-->
<!-- Connection String for SQL Server 2008/2012 - kept for backwards compatability - legacy modules
-->
<add key="SiteSqlServer" value="Server=(local);Database=DotNetNuke;uid=;pwd=;" />
would someone please help me to tell me what is wrong?
You have your connection string defined multiple times in the ConnectionStrings section of the web.config. Which is the proper connection string?
EDIT:
You likely have an issue with the PortalAliases and your IIS configuration. What is the URL that you are trying to access the website using? Make sure that URL is listed in the PortalAliases table, also make sure that IIS will respond to the URL as well.

Role provider not working with SQL Server Enterprise

I am getting the following error,
The user instance login flag is not supported on this version of SQL Server. The connection will be closed.
from the below line of code in my razor view(MVC5)
#if(Roles.IsUserInRole("User"){}
I have upgraded my database sql server from Express 2012 to Enterprise 2012 edition . After that I got above error. Since my application is EF code-first, database is created automatically but role provider doesn't seem to be working.
<add name="Foo" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Foo_db;User ID=sa;Password=******" providerName="System.Data.SqlClient" />
Check out this thread. It should help.
http://forums.asp.net/t/1939748.aspx
I have tested this, User.IsInRole is now working for me. Make sure you have this in your Startup class
Did you remember to put the application name in your web config?
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="ApplicationServices"
name="AspNetSqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
applicationName="DONT FORGET THIS PART" />
</providers>
</roleManager>

Simple Membership Configuration with default connection

how do you configure your MVC4 application to use an entity framework connection string which already exists to use for its default connection with conjunction with the simplemembership provider tables etc on an existing SQL server database. Im sure ive achieved this before but with a new internet template mvc4 solution i seem unable.
i am configuring this line in InitializeSimpleMembershpAttiribute
WebSecurity.InitializeDatabaseConnection("myEntitiesConnstring","mytable", "UserId", "full_name", autoCreateTables: true);
I can get a combination of either
"Unable to find the requested .Net Framework Data Provider. It may not be installed.
if i leave this section alone
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
or
Format of the initialization string does not conform to specification starting at index 0
with
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
If i comment out the defaultconnectionlibrary i get.
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)
This entityframework connection string is valid and does connect to the database correctly. In all the 3 above scenarios i get no activity however if i use sql profiler to trace the activity.
(Connectionstring)
<connectionStrings>
<add name="myEntitiesConnstring" connectionString="metadata=res://*/Models.my_Model.csdl|res://*/Models.my_Model.ssdl|res://*/Models.my_Model.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=mydb;persist security info=True;user id=sa;password=password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
cheers
tim
p.s.Jon Galloway's article suggestions dont seem to be helping me

upload ASPNETDB.mdf to shared hosting?

I am developing asp.net mvc2 application and I use asp.net membership provider which uses ASPNETDB.mdf database. I have also my own database and now I wonder how to upload these 2 databases to server? Should I upload them as .mdf file or should I use SQL server? I prefer using SQL server and if someone knows the shortest way to convert and upload these 2 databases it would help me a lot.
Thanks in advance,
Ilija
Funny I just finished doing the same thing. The basic steps are as follows:
From Visual Studio, load your .mdf and choose "publish to provider" to make a .sql file.
Open SQL Management Studio, open a connection to your database and load the sql file. Add a "use yourdbname;" on top to have it output the tables to your database, then run it.
Now you should have the full table structure. What's left is to modify web.config to read the new tables:
First the membership provider:
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a "
connectionStringName="ConnectionStringLoginInfo"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
applicationName="/"
/>
</providers>
</membership>
Now the role provider:
<roleManager enabled="true">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider"
type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a "
connectionStringName="ConnectionStringLoginInfo"
applicationName="/"
/>
</providers>
</roleManager>
And lastly the WebPart provider, if you use it:
<webParts>
<personalization defaultProvider="SqlDatabaseProviderDRDBLoginInfo">
<providers>
<clear/>
<add connectionStringName="ConnectionStringLoginInfo"
type="System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider"
name="SqlDatabaseProviderDRDBLoginInfo"/>
</providers>
</personalization>
</webParts>
In this example I called the connection string ConnectionStringLoginInfo, but whatever you name it, make sure you set it in the connection strings part. Not gonna paste that too :)
This all took me way more than I care to say, but when I saw my app working flawlessly with the App_Data folder deleted, that was quite the moment!
Your simplest option for a hosted solution (i.e. your hosting plan is not a Virtual Private Server) is to generate SQL scripts of your database, exporting these to *.sql files and then run them in your hosted SQL connection.
I would normally connect to my web host's SQL instance using SQL Server Management Studio and either open or paste in the scripts generated by my local copy.
Depending on whether or not your web host provides the service, you might also be able to use the "Publish to provider..." option in Visual Studio.
As a supplement to Blindy's answer I wanted to mention that another way to configure the providers is to change the connection string settings of the default ConnectionString used by most of the providers, which is LocalSqlServer. To do this you just override that particular ConnectionString in your web.config like so:
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="change this to be the details of your host database" providerName="System.Data.SqlClient" />
</connectionStrings>
Also, if you don't want to clear the entire connectionStrings section you can just remove the particular connection string like this:
<connectionStrings>
<remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="change this to be the details of your host database" providerName="System.Data.SqlClient" />
</connectionStrings>
This works, because all providers that default to using Sql Server for their Data Store - such as the membership provider - use the "LocalSqlServer" connection string by default. Thus, if you override it, you don't have to change each provider to point to a different Connection String.
Also, for security reasons, you might want to look into encrypting the connectionString section of your web.config file. The following two articles provide more info.
Encrypting and Decrypting Configuration Sections
How To: Encrypt Configuration Sections in ASP.NET 2.0 Using RSA