How to deploy mvc application to an hoster - c#

I am bulding an application with MVC.
Ont thing I don't understand, it's how to deploy to an hoster when building is finished and how to link database on the hoster.
Now I create the application and the database was automaticly create on first run of the application. I have create a dbcontext class, a model class (with entities) and a controller with actions.
The database was created but also a file database, I think not in sql server express.
No connection string was added in web.config, the database is in the app_data folder.
My questions are :
is that the database will be shipped within App_Data and the application will work without any further configuration in sql server on the hoster ?
is that the database will be created automatically at the first run on the hoster ?
If no, How to create a script SQL which will create the database and will be needed connection string in web.config
tks for help

is that the database will be shipped within App_Data and the
application will work without any further configuration in sql server
on the hoster ?
The answer is no because by default in web.config you have (LocalDb) which is a version of SQL Server that comes with Visual Studio. To make it work you have to install on server this version of SQL Server.
is that the database will be created automatically at the first run on the hoster ?
The database can be create automatically if you specify that connectionString to be over a version of SQL Server.
Note: If you use migration you can check at Publish Execute Code First Migrations
If you can install (LocalDB) on the host server, than you can leave the connectionString as it is.
If you use other versione of SQL Server you change the connectionString to be like below, the values from Data Source, User ID and Password must be changed to match your server connection
<add name="DefaultConnection"
connectionString="Data Source=ServerAddress;Initial Catalog=DatabaseName;Integrated Security=False;User ID=UserName; Password=UserPassword"
providerName="System.Data.SqlClient" />

Related

Attempt to attach an auto-named database when installing my app in another pc

I recently finished building this C# Winforms app that uses a localDB connection and when installing it on another PC, I get the error as shown in the title, even though I included the SQL Server Express localDB in the prerequisites folder.
I tried all kinds of solutions like changing the method of how to build the setup file (using Advanced Installer), but it seems that the problem is always related to the constant AttachDbFilename attribute of the connection string that's not changing according to where the database is newly installed.
Here's how the connection string is defined in the App.config file:
<connectionStrings>
<add name="client"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Surface\source\repos\VisaTurbo\VisaTurbo\Client.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Please I've been stuck with this problem for 3 days now and the due date to submit the application have already been reached.
Thanks in advance
You can by example check the database file exist in %userProfile%\DBName.mdf and create a new one with https://github.com/martincostello/sqllocaldb.
But this make the connectionstrings parameters obsolete :)
You can also generate a new one with SqlConnectionStringBuilder
But if the user move the database file this will reset the application each time.
That's why in general the database in on a server, not locally stored on the user's machine.

Publishing the project to Plesk Hosting

I have four projects in one solution
RapporAMS.Data (Contains the connection string and database access queries and entity framework installed, it also have connection string in app.config file same that of web.config in forth project)
RapporAMS.Service (Act as an interface between RapporAMS.Data and RapporAMS.MVC )
RapporAMS.Entities (Contains all the Entities and view models used in the solutions i.e business objects)
RapporAMS.MVC (MVC project front end of the application also have connection string in web.config and entity framework installed)
I have created the database in Plesk named "esssospa_AmsDB" with username and password. Then I used "import dump" to import .bak file. Now, it is showing file size and the total number of tables in the database.
I am not sure about database has been attached to esssospa_AmsDB or not
After that. I winziped only RapporAMS.MVC project and uploaded it to the httpdocs directory on the server.
Then I changed the connection string in web.config don't know how to change the connection string in app.config of RapporAMS.Data. This may be the issue of the error.
Problem 1: Database esssospa_AmsDB is not showing up in my SQL EXPRESS 2014 when I logged in using the credentials.
Problem 2: Having 500-Internal Error when I access the website, further screenshot is attached. click # Database on Server

How to change the connection string on another computer?

I am very new to SQL and tried to build a software by using SQLServer and Visual Studio. I created a setup file for my program by using InstallShield Limited Edition Project. I want my program to be used on other computers and I want every user to be able use their own databases installed on their computers. To me, when a user installed the program, the program will search for a connection string that I used while creating the program. Therefore, I think this connection string must be changed by users. How can I add such properties into my program? By the way, I used model first entity framework in my program. My connection string written in app.config is :
<connectionStrings>
<add name="otobusVTNesneleri" connectionString="metadata=res://*/OtobusVeriModeli.csdl|res://*/OtobusVeriModeli.ssdl|res://*/OtobusVeriModeli.msl;provider=System.Data.SqlClient;provider connection string="data source=PIPASO\PIPASOSERVER;initial catalog=otobus;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
P.S. I searched the internet about this problem and saw that people suggest that I use SQL Server compact. I want to solve this problem without using it. I have SQL Server 2012.
You shouldinstall the SQL Server with the same instance name in all the PCs. And then, in the connection string, instead of specifying the computer name, use the "local" syntax:
Data Source=(local)\yourInstanceName
It's not a good idea to let your users change the connection string.
Entity Framework starting from version 6 supports Code Based configuration. Refer to this article: Code-Based Configuration (EF6 onwards)
without use sql server username password use it.
<configuration>
<connectionStrings>
<add name="ConString" connectionString="Data Source=Trainee4-PC;Initial Catalog=Demo05-09-2014;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
use sql server username password use it.
<configuration>
<connectionStrings>
<add name="ConString" connectionString="Data Source=DENISH\SA;Initial Catalog=Demo05-09-2014;User ID=Demo;Password=Demo123" />
</connectionStrings>
</configuration>
local databases should be sql compact (sdf files). in any case, what you need to do is to make the installer modify the app.config file after it's deployed. this is what i'm doing in my current project. in my current project i have a local database (an sdf file) and an installer (created using WiX) that deploys my app. i configured the installer to modify some app.config settings after deploying the app. one of the settings was the connection to the local sdf database.
one thing to be careful about is that, while using sdf files, you cannot specify relative file paths in the config file. you need to either specify the full path (ie: d:\..\db.sdf) or use special folder names (ie |AppData|db.sdf). AppData will resolve to the current path of the executable.
requiring a full blown sql database on the client will also mean you'll have to install SQL Express on the client machine. you don't have to do this with sdf files.
in case you decide to use WiX you can modify your connection string like below.
<util:XmlFile Id="WindowsServiceUpdateConnectionString" File="[INSTALLFOLDER]$(var.Phoenix.WindowsService.TargetFileName).config" Action="setValue"
ElementPath= "//configuration/connectionStrings/add[\[]#name="PhoenixCacheEntities"[\]]/#connectionString"
Value="Data Source=[INSTALLFOLDER]Cache\PhoenixLocal.sdf"/>
you can see that an xpath is used here to determine the setting you want to change. hope this helps.
NOT RECOMANDED
its just becouse you said you are just starting in c#
so to write the connection string into a file :
using System.IO;//delcare it the top
File.Write("config.cfg","your connection string goes here");
to read From it :
using System.IO;//delcare it the top
string CNXSTRING = File.Read("config.cfg");
btw you have to create a form where the user and can type the new connection string lets say it have a textbox named txtstring the code will become
File.Write("config.cfg",txtstring.Text);
PS : use the write when you want to save the CNX String like in the save button of that form .

Visual Studio 2013 not creating database when I select the authentication option

I tried this on two different computers but get the same issue.
I am using the latest and greatest visual studio 2013.
I create a new project (of type Web->Asp.Net application)
Choose a template type of MVC or Web API (have same issue whatever type of template I select).
Then I change the authentication type to use individual.
The project with all the authentication code is created.
Web.config has the connection string set for a .mdf file.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20131031102303.mdf;Initial Catalog=aspnet-WebApplication1-20131031102303;Integrated Security=True" providerName="System.Data.SqlClient" />
But the mdf file is missing in the app data folder. I searched the whole computer for the mdf file, but it is not created anywhere.
As a result testing my sign in and register modules using a browser gives me the Yellow Screen Of Death with the db connection being the issue.
What am I missing here?
It should be in
C:\Users\[*yourusername*]\Documents\Visual Studio 2013\Projects\WebApplication2\App_Data
This is not a real answer to my question as to why this is happening. But here is the workaround I did to make it happen. I downloaded one of their sample projects which did have the mdf file in the app_data folder. Then I got the table schema from that mdf file and created those tables in my SQL Server database. Then I changed my connection string to point to my SQL Server database. My authentication pages worked after that with this new database. But what a pain to go through.

How to create a setup file for a project that is connected to SQL database?

As mentioned in the title of the question; I have a desktop application which is visual studio project that is connected to SQL database. I want to create an exe file (setup file) for this project, how can I attach the database with it?
Thanks.
Same problem with solution: Quick deployment of Visual Studio 2010 app with SQL database
If it's SQL server, I'd suggest scripting the database to a file, and use a custom action to call osql with that script against a database server specified in a variable.
Add the MDF to your project as a content file. The installer will copy content files to the install folder, so then it's just a problem of correctly using that database in the application. SMO might be able to help out there.
Make your database during Setup only, by Running Scripts for creation and intialization if needed
Use custom Scripts, and Custom Dialog Box to Achieve this.
You can use xcopy deployment with SQL Server Express databases. From the link mentioned:
To make your application work with the Xcopy deployment feature of SQL
Server Express, you must make sure that the connection string you use
in your application contains the appropriate parameters:
Use the data source parameter, but change the computer name to either
a period (.) or (local). You must also specify the name of the
instance, unless you are sure that SQL Server Express will always be
installed on an unnamed instance.
Use the initial catalog or database parameter, but do not set the
parameter to a value.
Add the AttachDBFileName parameter and set it to the name and path of
the .mdf file. Attachdbfilename is a SqlClient connection string
option that enables attaching databases at runtime and autogenerates
database name. The DataDirectory keyword lets you specify the relative
path of a database file. Attachdbfilenamealso helps with database
portability. For more information about Attachdbfilename, see the
Visual Studio 2005 documentation.
The following connection string will attach the MyDb.mdf database
file, which is in the same folder as the application executable, to
the SQL Server Express instance running on the local computer.
#"Data Source='.\SQLExpress'; Initial Catalog=; Integrated
Security=true; AttachDBFileName='" |DataDirectory| +
#"\MyDb.mdf'"

Categories