I am learning basics of MVC. And forgive me If I am not able to ask question properly because I am not aware of various technical terms. Anyways I am trying build one simple page where I will have two tables courses and Instructors. I am able to do most of the stuff and it seems to be working. Also I do have a solution given by my faculty of the same problem. When I am comparing my solution and faculty solution then in the web.config file of the actual solution given by my faculty I see something like
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcCRUDwithSQL-20140217025002.mdf;Initial Catalog=aspnet-MvcCRUDwithSQL-20140217025002;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="CourseContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=CourseContext-20140217145250; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|CourseContext-20140217145250.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
Where as in my solution that I just have one add name tag. I don't know why the another tag did not get created automatically. Or do I need to add that tag manually.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication9-20150313042831.mdf;Initial Catalog=aspnet-WebApplication9-20150313042831;Integrated Security=True" providerName="System.Data.SqlClient" />
Have I done something wrong that the connection string related to CourseContext did not get generated automatically. Or we need to add for CourseContext manually? Please help me. Since most of the files and its content are generated automatically I must have done something wrong that connection string pertaining to CourseContext did not get generated.
Use the steps below to generate the connection string via visual studio server explorer
Click on server explorer
Connect to database
Choose server name from dropdown
Choose your authentication type
Select your database from dropdown
Test connection
Copy the connection string from the properties and paste in your web.config
Also take a note of #Guffa answer, he's got a very good point
You don't need one connection for each table, you only need one connection for each database. A database can contain many tables (the project I'm working on right now has 9 tables, and that is a really small project).
The Visual Studio template starts out with a default connection string, but no database. It looks like it's that connection string that is still left in the solution.
Related
I am at a loss as to how to fix this issue.
When running my MVC application and trying to access the user database I get the dreaded SQL Network Interfaces, error: 50.
I can see all the data in the Server Explorer in VS, but I can never get the application to see it.
I have spent sometime reading this;
SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance 9
I have renamed and created new databases.
I have added/changed the entry in the applicationHost.config file.
I have changed the connection string every which way from Sunday.
I just cant figure it out.
Here is my connection string in the Webconfig file.
<add name="UserConnection" connectionString="Data Source=(LocalDB)\V11.0;AttachDbFilename=|DataDirectory|\ShopUsers.mdf;Initial Catalog=ShopUsers;Integrated Security=True" providerName="System.Data.SqlClient" />
I even downloaded and ran the ContosoUniversity sample code to see if it works, and it does.
Here is that connection string, (that works fine)
<add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
It seems there is something amiss between IIS and the db Server.
Any new ideas would be greatly appreciated.
Oh ya, I did have this working just fine on my old PC.
Seems like you have the database file created already (I suspect you do because of the following segment: AttachDbFilename=|DataDirectory|\ShopUsers.mdf;). If so, then ensure you have it in the same folder beneath your project as the original one on your old PC, usually the |DataDirectory| stands for App_Data (look the answer to this question).
If, you need a brand new database however, you should have tried the following connection string:
<add name="UserConnection" connectionString="Data Source=(localdb)\v11.0\ShopUsers;Initial Catalog=ShopUsers;Integrated Security=True" providerName="System.Data.SqlClient" />
This will create the database for you.
Im working on a MVC Application and so far ive been using localDB since that was which was included in the tutorial. I want to switch the application now to my SQL Server but im not sure how to.
I get that I have to change the connection String. But not in what way exactly since the SQL Server has a username and password which my localDB doesnt.
Another question regarding this is, do I have to create the tables myself in the beginning on the SQL Server or will they be generated by the entity framework like in my localDB?
Current connection string for localDB:
<add name="AcquisitionDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Acquisitions.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
Im pretty new to this so any help is very much appreciated
if you are going to use MS SQL then it wil look something like this:
<add name="AcquisitionDBContext" connectionString="Data Source=hostname\sql_instance_name;Initial Catalog=databaseName;Persist Security Info=True;User ID=username;Password=password;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Where "hostname\sql_instance_name" is the servername(or ip)/the sql instance name eg. localhost\sqlexpress
ConnectionStrings.com is the best reference for connection-string syntax. It shows you the options to use when you have a username+password. In this case:
Server=myServerAddress; Database=myDataBase; User Id=myUsername; Password=myPassword;
Entity Framework will not create your tables for you unless you tell it to by calling something like this:
Database.SetInitializer( new DropCreateDatabaseIfModelChanges<YourDbContextHere>() );
(Where Database is the System.Data.Entity.Database type).
Try it:
<connectionStrings>
<add name="<<Name of Connection String>>" connectionString="Data Source=<<Put here path to your SQL SErver>>;Initial Catalog=<<Database name>> ;Integrated Security=SSPI;Trusted_Connection=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
Here's how connection string should look like (tweak to your needs)
<add name="DefaultConnection" connectionString="Data Source=server_name,port; Initial Catalog=your_catalogue_name; Integrated Security=False; User ID=userName;Password=Passwd; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
Before first run I would recommend using migrations.
Go to PackageManagerConsole (if you don't have it in menu, install NuGet first via Extension and Updates)
Then inside console type Enable-Migrations (if you didn't already).
Next type Add-Migration your_migration_name and then Update-Database to let migrations create tables for you.
If you change something in your model files later, you can again type Add-Migration your_migration_name and Update-Database to update database with those changes.
This is my first MVC application so please be patient and kind. I have created an ASP.NET MVC 4 Web Application in Visual Studio 2012. I created the database code first, so all I did was create the model and a data context and a SQL server database was created for me. Every time I changed the model I could just update the database. I also connected to an Oracle database get other data that I needed. These are the connection strings that were used:
<connectionStrings>
<add name="ApplicationDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=something;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="ConnectionString" connectionString="Data Source=something;Persist Security Info=True;" providerName="Oracle.DataAccess.Client" />
</connectionStrings>
However, the database related to the model needs to be in Oracle (and I need a script for it but that is next weeks question) not SQL.
I was hoping that I could just change the data context provider name in the web config like so:
<connectionStrings>
<add name="ApplicationDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=something;Integrated Security=True" providerName="System.Data.OracleClient" />
<add name="ConnectionString" connectionString="Data Source=something;Persist Security Info=True;" providerName="Oracle.DataAccess.Client" />
</connectionStrings>
But that is not working - it gives me errors.
Please note that I am NOT asking about OracleMembershipProvider. This question is unique in that I want the model to relate to an Oracle database. I am not worried about membership and user authentication etc. I have already taken care of that.
It seems like it should be simple enough, but I have googled until every link I find is purple.
I need you to tell me the steps and what to do (hopefully in the web config) to get my model to 'create' an Oracle database. Please.
After reading through a ton of articles I found out that Oracle doesn't support code first (yet) and although there are some work-a-rounds it’s probably best to create scripts to create the db for us.
So as an alternate solution we to created the database in Oracle first and then connected to it from the MVC application.
We added an ADO.NET Entity Data Mode and in the EDM Wizard we said "Generated from database" and added a new connection to the Oracle database. You need Oracle Developer Tools for Visual Studio for this.
Add a new controller and for the data context class, choose Entities. Don't forget to add the connection string to the web config.
<add name="Entities" connectionString="...;provider=Oracle.DataAccess.Client;provider connection string=...;data source=...;" providerName="System.Data.EntityClient" />
This should be done before you start coding because you no longer have a model so I have had to change my code quite a lot. Not more than a day or two's work though.
I'm trying to implement basic three tier architecture with mvc 3. but getting an error while running the solution ( showing no error while building the solution). The organization of my projects & error message can be seen in the attached image.
Please help.
OK guys, This is an update for.
As specified by some comments, I copied the original connection string from app.config to my web.config file & getting this error as shown in the image.
I tried to search a lot but no clue! Any help will be highly appropriated.
#Maarten is right. you always copied the normal connection string (that is a part of entity-connection string). you need to copy the entire connection string that was generated in your App.config file in Data layer and paste it in the web.config file in your presentation layer. the entity-connection string like as :
<add name="NorthwindEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sqlexpress;initial catalog=Northwind;persist security info=True;user id=sa;password=Blister01;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
don't copy-paste it, it is just a sample
The connection string you've copied is a normal connection string. You are using an edmx file which means you need an entity-connection string.
You need to copy the entity-connectionstring into the web.config. You can find the EF-connectionstring in the app.config file in the Data layer where EF added it for you.
you just add connection string connectionStrings section in web.config in Presentation layer!
See this image
EDITED: try with this
<add name="NORTHWINDEntities" providerName="System.Data.SqlClient" connectionString="data source=(local)/sqlexpress; initial catalog=NORTHWND;Integrated Security=SSPI" />
I am just learning MVC and I wanted to store my data in SQLEXPRESS instead of the MDF file that's provided.
I went online and learned that I was supposed to change Web.config, so I commented out and replaced the connection string like so:
<connectionStrings>
<!--<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />-->
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=BuySellDB"/>
</connectionStrings>
I was hoping that System.Security.Membership would automatically create a new database and table to hold my information, but it doesn't -- instead, it just barfs because it can't find a database there. How do I configure it to create a new database and table if none exists?
Edit: I was watching tutorial videos on asp.net/mvc and I saw how DropCreateDatabaseIfModelChanges can be used to auto-create or re-create the necessary database and schema. I was hoping there was some way to include the aspnet_* tables in that....
You have to create the database by yourself using the command promt.
Start that command promt and execute "aspnet_regsql". A Wizard will start
and help you create the database.
Here is nice sample =>
http://www.ezineasp.net/post/How-to-Create-Aspnetdb.aspx
If you want to give the project away. Think about
create the database as i told
create a sql script from that
build the database using SqlClient in your application on first startup
You could check if the database exists in the global.asax "Application_Start" Method
protected void Application_Start(Object sender, EventArgs e)
{
//... check if your database already exists, if not DO IT ;)
}
In the scenario that you want - your database IS a valid sql express MDF file.
This is usually the best way to share the solution otherwise you are going to have to worry about database config, setting up the db, which server, etc. If you still want to do that you'll want to right click on your data connection in the server explorer in visual studio and publish the database and generate scripts if you have anything custom otherwise use aspnet_regsql.
However I still strongly suggest if you are sharing this application you keep it in the app_Data folder and let other users choose if they want it to run elsewhere. They can publish it as they see fit using the same method.