how to create tables on azure and connect them with VS2013 - asp.net-mvc

I have built my MVC 5 project with a database using entity framework db first. I published my website but had problems with my database.after lots of research to solve the problem,i figured to build my database on azure since it's only 2 tables and copied the connection string to the web.config file on my MVC 5 project.
when publishing again with the new connection string update, an error showed indicating that can't find the database:
The system cannot find the file specified
Exception Details: System.ComponentModel.Win32Exception: The system cannot find the file specified
This is what the azure offered me as a connection String:
Server=tcp:idud662mdp.database.windows.net,1433;Database=sportsMArAiCkhuw;User ID=sportsMania#idud662mdp;Password={your_password_here};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;
I have modified it and this is my connectionString:
<add name="DataEntryNew" connectionString ="Data Source=idud662mdp.database.windows.net;Database=sportsMArAiCkhuw;User ID=sportsMania#idud662mdp;Password=My-Pass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient"/>
What's going wrong?

Your connection string is not the same one as provided by Azure. Try changing the DataEntryNew tag in the web.config file to:
<add name="DataEntryNew" connectionString ="Server=tcp:idud662mdp.database.windows.net,1433;Database=sportsMArAiCkhuw;User ID=sportsMania#idud662mdp;Password={your_password_here};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient"/>

Related

How to use existing SQL database in my separate project under one solution in ASP.NET MVC application

I have created 2 separate projects under one solution where one project is a class library. I have created an ADO.NET Entity Model from my existing database into this. Another project is an ASP.NET MVC web app.
I have used Individual User Authentication and wanted to use registration and login facility provided by this.
Now I am unable to use my existing database as this project is creating new database by itself when first runs.
Please guide me how to use existing database. I have given reference to my ASP.NET MVC project. These are my connection strings. Please help me.
<connectionStrings>
<add name="LAPTOP-71D4QMB6"
connectionString="Data Source=LAPTOP-71D4QMB6;Initial Catalog=VCAdvertiserDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="HBDBEntities"
connectionString="metadata=res://*/Data.HBModel.csdl|res://*/Data.HBModel.ssdl|res://*/Data.HBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=LAPTOP-71D4QMB6;initial catalog=VCAdvertiserDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Error thrown is:
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)
You have to copy the connection string from the app.config of your class library to the web.config of the MVC project. You need to put it in both the root web.config and also have to put it in the shared web.config under the shared views folder. That last may not be needed but it also doesn't hurt.
That worked for me.
Joey

FSharp.Data.SqlCient works locally but not on production server

I am having an issue with FSharp.Data.SqlClient. I am building an Asp.net Core full framework 4.6 web app. The main project is in C# which references several projects that are in F#. Each of these F# projects use FSharp.Data.SqlClient for data access. When running the app locally everything works fine, but when I deploy it to azure I get the error "Instance Failure" when the F# code attempts to execute a query.
I have a suspicion that in some way it has to do with how the connection strings are being consumed or maybe some sort of runtime conflict between FSharp.Data.SqlClient and Entity Framework. Entity framework is in the main project to handle the membership data and nothing else. I have to specifically add a connection string to a config file in the main project so that the referenced F# projects can access the database at runtime. Entity framework consumes it's data string via the AppSettings.Json file. I am unaware if there is a better way to wire this up, but again, this works perfectly when I run it locally but not on the server that it's been deployed to.
Is there something that I need to enable or change code-wise for the app to work on the production server?
Here's a view of my data strings. In my F# project I have an app.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data
Source=server code here"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
</startup>
</configuration>
In that same F# project I have another file that I use to access the runtime connection string:
module internal DbAdmin
open FSharp.Data
open FSharp.Configuration
module Admin =
// runtime connection string
type private Config = AppSettings<"App.config">
let rtConnection = Config.ConnectionStrings.DefaultConnection
In the main C# project I have an app.config file:
<configuration>
<runtime>
<gcServer enabled="true"/>
</runtime>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=server
code here" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
and in the appsettings.json is configured as this:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=Server code Here"
}
}
this is the actual query code:
type Select_AllArticles =
SqlCommandProvider<
"
select * from article.vw_AllArticlesAndDetails
", Admin.connectionString, ConfigFile = Admin.configFile
>
try
succeedWithMsg
(Select.Select_AllArticles.Create(Admin.rtConnection).Execute() |>
Seq.toList)
(createGoodMsg(OK("Some Success Message.")))
with
| ex -> fail (createBadMsg(DbError(ex.Message + " --selectAllArticles")))
**Update: **
After remote debugging the app, this is the entire error message it gives
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 is odd to me as the connection string in use is the same one used by Entity Framework which seems to access the data base just fine.
New Update
Ok, I think I figured out what's going on. I believe it is definitely a config issue. So as I stated before, in development I had to create a config file in order to create a connection string that FSharp.Data.SqlClient could connect to. I couldn't figure out how to get it to connect to the connection string in the appsettings.json file. Perhaps someone can explain that to me, as that may be the best solution. Anyhow, I followed up on my assumption that the connection string inside of the config file wasn't getting updated on deployment by manually inserting the production server connection string, and then deploying the app. Sure enough the issue was gone, and everything worked normally. So, now the question is, what's the best way to get FSharp.Data.SqlClient connected to the connection string correctly in a core app that utilizes a appsettings.json file? How do I go about handling this issue? I need someone to walk me through it, as I'm new to this.
Current Status
So after realizing that it is indeed a config issue, the question now is how do I properly retrieve the connection settings from the appsettings.json file via my F# projects. Using the FSharp.Data json provider, I need to figure out how to properly locate the appsettings.json file for both production and development use. How can an F# project locate a file in the main project? Am I just overly complicating things?
You appear to be looking for App.config to find the connection string. That usually gets replaced with a different .dll.config file when deploying, though that doesn't appear to be the issue. Do you have a DefaultConnection in you Azure App Service connection strings that is incorrect? Other than that, I would suggest parsing the connection string from the appsettings.json rather than the app.config file.

MVC project database cant find

I'm a beginner in MVC application development. I'm just trying to make a MVC application getting help from internet. That was a simple example and working properly. The article is said the database is in app_data folder. But actually there is no any database in that folder. Then I just tried to find the physical database location using the connection string.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-ePhoneBook-20140322204146;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
as the above connection string (I just not write down this and it is auto generated code in web.config file), the database name is "aspnet-ePhoneBook-20140322204146". Then I checked weather this DB available in physical location of the sql databases. My all other databases (My old projects' databases) are in that folder. But couldn’t find this database. then I tried with folder searching option in windows 7. There is no any database with that name. The wonder is my application run properly. Data saved properly. SQL management studio also not showing the database.
My question is, How to find the physical location of the database and why is this database not showing in SQL management studio.
The issue is you're pointing to SQL server, and ,definitely, not to separate attached database file.
your connection string should look like this:
<connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-ePhoneBook-20140322204146;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ePhoneBook-20140322204146.mdf" />
where |DataDirectory| - is actually App_Data folder in terms of ASP.NET.

Which connection string WSAT uses

I create a new ASP.NET MVC project with internet template.
I build the solution.
I open ASP.NET Web Site Administration Tool (WSAT).
I click the security tab
I get this error:
"Unable to connect to SQL Server database."
I am using Visual Studio 2012 and SQLServer Express 2012.
I don't have IIS installed (other then what ships with Visual Studio).
The Connection String from the new project:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication2-20130804051506;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication2-20130804051506.mdf" providerName="System.Data.SqlClient" />
It is worth mentioning that I did not change anything in the web.config after its initial creation and that MVC project seems to be working properly e.g. I am able to register and login and then see that data in the (LocalDb)\v11.0.
I read on other posts that the connection string from machine.config is sometimes used instead of the one from web.config.
I have a two machine.config files, one under C:\Windows\Microsoft.NET\Framework\config\v4.0.30319 and the other under C:\Windows\Microsoft.NET\Framework\CONFIG\v2.0.50727
When is a machine.config's connection string is used instead of my web.config's connection string?
I want to use the (LocalDb)\v11.0 data source, why does WSAT unable to connect to it?
From http://forums.asp.net/t/1483981.aspx/1 :
by default, the membership / role provider uses the "localSQLServer" connection string which is set to point to a local SQL Express database from the root web.config. In most cases, the server do not have SQL express installed and you will get this error.
By clearing the connection strings should reveal those errors.

MVC 3 Connection String Confusion

Using VS 2010, MVC 3, and SQL Express, I've been following the tutorials on ASP.net. I'm also using EF code first. So far, my connection string has been:
<add name="MusicStoreEntities"
connectionString="Server=.\SQLExpress;Database=MusicStoreDB;Integrated Security=SSPI;User Instance=true"
providerName="System.Data.SqlClient" />
Which creates the db in:
C:\Documents and Settings\username\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS
I now want to have the database reside in the App_Data folder. So I created a new web application following a different tutorial, but this time I added a SQL database to it, and modified the connection string for the membership database by changing the database name. I then used aspnet_regsql.exe to create the membership tables. I then created POCO classes, a controller and added seed data. Since the database already existed, it complained about not having an EdmMetadata table. Upon researching that I realized my mistake and deleted the database. Not to mention the DB will be dropped and created as the project is modified. Running the project again gave me the error "Initial catalog not specified" so I modified the connection string. Running the project again I get the error 'C:\ContosoUniversity\ContosoUniversity\App_Data\School.mdf' already exists. Choose a different database name.
A search of my hard drive reveals this database does not exist. My connection string looks like this:
<add name="SchoolContext"
connectionString="data source=.\SQLEXPRESS;Initial Catalog=School;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|School.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
What is making it think the DB exists? What do I need to do to get code first to create the DB in the App_Data folder?
Although I ended up changing the database name in frustration, a search of my registry revealed multiple entries for school.mdf in the app_data directory. Removing these solved the problem.

Resources