Create database on Azure using Entiitty Framework - asp.net

I want to create ASP.NET MVC app using database on Azure. I have already written model classes for database. But how can attach this database or tables on azure?? It's connections string for my already created DB, which i want to use.
<add name="AccomodationContext" connectionString="Server=tcp:student-docs.database.windows.net,1433;Database=docDb;User ID={username};Password={password};Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" />
It's empty, I would like to use it to has a tables created from my model classes, or create a new database. But I can't find, how should look correct connection string. I know that for LocalDB there is 'AttachDbFilename' in connection string.

You should have a connection called DefaultConnection in your web.config that you can place your connections string in.
You can get the actual connection string directly from your Azure portal.

Related

Using named instance in connection string

I want to upload my DNN site in customer's host, they use SQL Server 2005 and they use named instance, how can I use named space in connection string ?!
<add key="SiteSqlServer"
value="Data Source=79.175.164.226,2005; Initial Catalog=jdmedu; User ID=; Password=;" />
DNN shows database connection error.
Can you check that you can construct ODBC Connections to the DB (outside of your application).
I Suspected that, the problem is in the Configuration of the DB Instance and it can be solved from the SQL Server Configuration Manager.
Also, can you check
http://www.connectionstrings.com/sqlconnection/connect-via-an-ip-address/
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

can't find my users AspNetUsers

I've moved all the tables,
AspNetRoles
AspNetUserClaims
AspNetLogins
AspNetRoles
AspNetUsers
into my working SQL Server 2012 Express database with the rest of my tables and updated my .EDMX in Visual Studio Express 2013. I can now see all my tables and all my models in one place in VS.
When I create a new user everything works fine but the new users are not ending up in the AspNetUsers table in the database. Like I said, the application continues to work fine with the new user but I can't find them anywhere, where are they?
I only have one connection string in my application, so I have no idea where or why they could be going anywhere else:
<add name="Entities"
connectionString=
"metadata=res://*/Models.Listing.csdl|res://*/Models.Listing.ssdl|res://*/Models.Listing.msl;provider=System.Data.SqlClient;
provider connection string="data source=XXXXXXXX;initial catalog=database;
integrated security=True;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
The new user is somewhere, after creating one I can navigate. Could they just be stuck in memory or something?
Can anyone help me out on this one?
The new user is probably sitting in the database specified in your default connection string in your web.config. Make sure to specify the connection string named "Entities".
I'm guessing but I would tend to believe that they are being saved in a LocalDB file specified by your default connection string.

How to construct ConnectionStrings

Could you explain me how to construct correct ConnectionStrings? I mean that one you can find in web.config file in MVC project. I understand that if you want to add a new connection string you have to write <add ... /> XML tag with parameters such as name, connectionString (it is the most interesting parameter for me) and providerName (perhaps some else?). What each parameter does and means? How to construct connectionString parameter? Where is the db engine indicated?
The questions above are only examples. I care about collecting the most amount of information about constructing ConnectionStrings.
Starting from NET 2.0 you have at your disposal a class named SqlConnectionStringBuilder
Its purpose is to help building dynamically the connection string. But the various properties available explain in great detail the functionality underlying to each possible setting
The SqlConnectionStringBuilder class is derived by a base class named DbConnectionStringBuilder and this allows all the ADO.NET provider to implement their own version of this class. In the link provided there are the references relative to other ADO.NET providers
If you are trying to construct entity framework connection string just use as follows.It will help you..
string connectionString = new System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
System.Data.SqlClient.SqlConnectionStringBuilder scsb = new System.Data.SqlClient.SqlConnectionStringBuilder(connectionString);
EntityConnectionStringBuilder ecb = new EntityConnectionStringBuilder();
ecb.Metadata = "res://*/Sample.csdl|res://*/Sample.ssdl|res://*/Sample.msl";
ecb.Provider = "System.Data.SqlClient";
ecb.ProviderConnectionString = scsb.ConnectionString;
UPDATED:
The easiest way to get the connection string is using the Server explorer window in Visual Studio (View-->Server Explorer menu) and connect to the server from that window. Then you can see the connection string in the properties of the connected server (F4 with your connection selected).
If you create the database in SQL Server Management Studio, that database will be created in a server instance, so that, to deploy your application you'll have to make a backup of the database and deploy it in the deployment SQL Server. Alternatively, you can use a data file using SQL Server Express (localDB in SQL Server 2012), that will be easily distributed with your app.
I.e. if it's an ASP.NET app, there's an App_Datafolder. If you right click it you can add a new element, which can be a SQL Server Database. This file will be on that folder, will work with SQL Express, and will be easy to deploy. You need SQL Express installed on your machine.

Code first: Where's the connection string & the Database?

I'm testing how code first works. Here's how I defined the Context
public class EfDbContext: Context
{
public DbSet<Client> Clients { get; set; }
}
I didn't create any data base, but I was able to do all the CRUD operations.
Now I don't see the connection string in my web.config. I don't see either the Database. I've checked the App_Data Directory and also the Sql Server Express. I don't see any trace of the Database created.
Yet, everything is perfectly.
EF code-first will use a connection string that has the same name as your DB context - so you could define it like this:
<connectionString>
<add name="EfDbContext"
connectionString="server=YourServer;database=YourChoice;Integrated Security=SSPI;" />
</connectionString>
The database won't be created by default, until you actually do something, e.g. it should show up as soon as you make a call to EfDbContext.SaveChanges() for the first time.
It will be called the same as your DB context (YourNamespace.EfDbContext) if you haven't defined your own, custom connection string, and it should show up in your default local SQL instance.
See from the ADO.NET EF 4.1 Code First Walkthrough:
Where’s My Data?
DbContext by convention created a database for you on
localhost\SQLEXPRESS. The database is named after the fully qualified
name of your derived context, in our case that is
“CodeFirstSample.ProductContext”. We’ll look at ways to change this
later in the walkthrough.

MVC 3 ASPNETDB.MDF SqlExpress Database Connection String on Web Hosting

I think this is more of a connection string issue that MVC 3, so sorry in advance.
I've created a website in MVC 3 with the default database that is created when you register a new user. It works fine local but not on my web hosting where I receive the following error:
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)
The default connection strings are:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="ASPNETDBConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
Should I specify a connection string with an explicit IP address of the server or retain the .\SQLEXPRESS command? If I need to specify a username and password then what should I use? I've never used a local SQLExpress database before so I'm clueless (which I'm sure is apparent).
If you cannot use SQL Express, then you could consider an embedded SQL Engine:
SQL Compact Edition or SQLLite
I've never used SQL CE, but SQLLite is just a single DLL.
Obviously you don't have all the features of a fully fledged database engine, but they are still pretty powerful.
Generally you will need use a connection string with a specific IP Address, Username, Password, etc.
Who are you hosting with? Generally a .net host provider will give you the connection string to use to connect to your database. You can copy and paste in to you web.config. Also, when they create the database for you, they generally specify the username, password, and database name so you will have to change all of that.
This is visual studio has Web.config.debug and Web.config.Release with the xml transformations. This way when you publish to the site, the connection string is automatically changed from your local connection string to the connection string used in your production environment.
You probably need to follow this article?
http://www.asp.net/mvc/tutorials/authenticating-users-with-forms-authentication-cs
Deafault ".net" installs Application Services to "AttachDBFilename=|DataDirectory|aspnetdb.mdf"
Then you create your own DB and now you're using two DB's to store data that could just be in one.
If you read the above article it shows you how to combine everything into one DB.
When you deploy to the hosting server (and it has a limit on how many DB's you can use or isn't even running sqlexpress) neither DB attach. So you need to make a "transform file" for your web.config file (usually Debug and Release) which is basically another web.config but only with the lines of config in that you need to change when the site is deployed to the hosting server.
So your hosting provider will give you the details needed for the connection string on the server. You then put that in the "web.config.release" file and when you compile and publish the "release" version of your site you will get a web.config file that has the correct connection string data in.

Resources