I'm trying to learn MVC 5 and I have problem to connect to my database (SQL Server) I searched and find some solution but didn't work for me. So I want to know I have to create a database, a table and diagrams first, before I start to programming Asp.Net MVC 5 web application?
You can connect to your SQL Server using SQL Server Management Studio and create a database and a user for your application.
You then add the correct connection string an put it in your web.config. This depends heavily on your database setup. Here is a basic example (you need to at lease fill in Servername, UserId and Password):
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=False;User Id=userid;Password=password;MultipleActiveResultSets=True" />
More options can be found in the Documentation
If you use one of the CRUD templates, the database will be created through Entity Framework for you locally. If it's not connecting to the generated database, you can try the following code in the Application_Start method:
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseIfModelChanges<schoolcontext>());
using (var context = new SchoolContext())
{
context.Database.Initialize(force: true);
}
}
Related
Background: I have created a basic entity framework repository pattern using code first. I then created a test project and the first unit test created was to test some basic repository calls against the database. Strangely, I noticed that entity framework was creating a database in my users\myusername directory!
After a bit of research I realized best practice would be for me to create a database for a bit of testing using SSMS with a defined username and password. I accomplished this, and also tested a created connection in Server Explorer within VS 2012, which succeeded. My connection string as is looks like:
<connectionStrings>
<add name="TestConnection" connectionString="Data Source=.\SQLEXPRESS;User ID=DevSqlUser;Password=devsql;Initial Catalog=TestDatabase;MultipleActiveResultSets=True;Integrated Security=False;" providerName="System.Data.SqlClient" />
</connectionStrings>
However, now when I run my test which should instantiate a repository using the connection string by name, I get a strange exception which is using my windows credentials! Why it is doing this? I have no clue. I do not have any identity impersonation in my web config.
System.Data.SqlClient.SqlException: Cannot open database "TestConnection" requested by the login. The login failed. Login failed for user '{domain}{myusername}'
Anyone know why it isn't using the user ID I specified in the connection string?
Looks like your SQL express is set for Windows authentication only. Take a look at authentication mode settings on your SQL database. If you are using windows authentication, there is no need to provide username and password. If you need to use a username and password, you can use mixed mode.
Connect to database from SSMS, right click on server and select properties. Under security, you can see authentication mode settings. Set that as per your needs.
The answer is embarrassing! Even though all the code for the classes is in the project I was testing for, the connection string had to be copied from the web.config into the app.config. Wow I feel like a dumbdumb what a rookie mistake.... lol I was churning on this for hours this morning!
I am trying to create a database for an MVC 4 application. I used Entity Framework Code first approach. After digging into the problem I realized that it was not a connection string issue. I downloaded Sql Server Data tools and tried to create it from there but I get the same exact error which is related to Windows Authentication. I am not sure what is causing this problem, I even tried running as admin.
Any ideas??
I have tried mostly all forms of connections strings available online, the last two that I tried were
<add name="Request" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Requests;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Requests.mdf" providerName="System.Data.SqlClient" />
<!--<add name="RequestsContext" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|\Requests.mdf" /> -->
I am trying to create a local database, I have asked a question about entity frame work right here thinking that the problem was from there but now I know it has nothing to do with it. Here you can see all of the details about the models I created and I am trying to generate the database from.
Using Entity framework with SQL Server 2012 (local *.mdf database)
You don't have SQL Server installed on your PC. Install Express Edition and you should be fine.
EDIT: Use connection string for local server:
<add name="Request" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=Request;Integrated Security=True;" />
If its local, probably SQL Server service is not running. Start>cmd and services.msc will open services page. Right click on sql server and start
VS2010 C# SQL Server 2008 Express
I developed a couple of apps using Entity Frameworks 4.1. I used the database first method (I think) in that I created a database in Visual Studio 2010 and then created a model and finally the code from the model to access the database. This worked and I found that the IDE and the application uses a user instance of SQL Server 2008 Express.
I found out that User Instances are no longer in vogue and have been deprecated. So I guess I should move the database base files to the data directory of SQL Server. Easy enough.
But now will the EF infrastructure continue to work if I remove the User Instance from the connection string and point it to the main SQL Server? Do I have to regenerate the model? Is there some other course of action I should take?
You can set the connection string for your application using the app.config or web.config files.
Add the connection string as you would for any other application:
<configuration>
<connectionStrings>
<add name="sampleName"
providerName="System.Data.SqlClient"
....
Next, for your DbContext you Point it to your connection string:
public class sampleContext : DbContext
{
public sampleContext() : base("name=sampleName")
}
With these you should be able to set any configuration string you like. With this in mind, I'd also like to point you to this article on migrations for plating your database: http://msdn.microsoft.com/en-us/data/jj554735
I have a website project (C#/ASP.NET) opened in Visual Studio 11 (beta) which works with the built-in SQL Server Compact Edition. But, I would rather use my SQL Server 2012 which is installed on this machine, and I have my tables already created in it.
Question is, how do connect to it from VS11? Do I add it in the App_Data folder where I have the Compact database?
Right now I am opening my pre-made database with the
var db = Database.Open("StarterSite");
command.
If you already have the database created on SQL Server 2012, then just use it!
No need to add it to your project (and most definitely don't copy it into App_Data!).
Just create a connection string in your web.config something like:
<connectionStrings>
<add name="YourConnectionStringNameHere"
connectionString="server=(local);database=YourDatabase;Integrated Security=SSPI;" />
</connectionStrings>
and then use that in your app using all the normal ADO.NET components like SqlConnection or SqlCommand:
string connectionString = WebConfigurationManager.ConnectionStrings["YourConnectionStringNameHere"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("your-sql-statement-here", conn))
{
// do work in your database here....
}
Or use something like Entity Framework to make your life even easier!
It will depend on the technique you wish to use to connect to the SQL server database. I prefer to set my connection string in the web.config and use that when connecting to my database. There are several options you might want to explore. Here is a good MSDN article explaining some techniques.
Connecting to Databases in ASP.NET
So I'm moving my Asp.net mvc web app over to Arvixe shared hosting. This is the first time I've deployed an MVC app. I have been using SQL Server 2008 Express for the development database. Arvixe provides SQL Server 2008 or MySQL hosted databases.
A couple questions:
1.Can I use the mdf files from my Express database with the new Non-Express prodcution DB?
2.I'm having issues with my connection string. I changed the original web config connection string from this:
<add name="Database1ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
to this:
<add name="Database1ConnectionString" connectionString="Data Source=.;Integrated Security=SSPI;Initial Catalog=ProdsDB"
providerName="System.Data.SqlClient" />
Now I'm getting this error:
Cannot open database "ProdsDB" requested by the login. The login failed
I have setup the database called "ProdsDB" through the Arvixe control panel and added one user. Do I need to add the credentials somewhere in the connection string?
Yes. You're going to need to add User Id=myUsername;Password=myPassword; to the connection string, and you'll need to remove Integrated Security=SSPI
Of course, you'll need to set them to the username and password you created. Also, sometimes hosting providers host the database on a separate server. If that's the case, you'll have to specify the servername in place of the dot.