How do I connect to a Local database using asp.net Mvc4 and visual studio express 2012? - asp.net-mvc-4

I am following the Mvc4 OdeToFood tutorial on Pluralsight, which uses asp-net Mvc4 and visual studio express for web 2012.
I am trying to set up a small local database to hold some information so that I can proceed with the tutorial, but I am having trouble getting the database versions and connection string right, I have never done anything like this before.
I have been going fine until a few days ago when I tried to connect to a Local database.
Unlike in the tutorial, In my web.config file I had no connection string tags, The v11.0 database instance was specified in the tutorial, which from google seems to be a local database used for development purposes. so I looked up what I needed and entered into the Web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=
(LocalDb)\v11.0;Initial
Catalog=OdeToFoodDb;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|\OdeToFoodDb.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
This would not work for me, so upon more googling I tried:
<connectionStrings>
<add name="OdeToFood" connectionString="Data Source=
(LocalDb)\MSSQLLocalDB;Initial
Catalog=aspnet-OdeToFood;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|\OdeToFood.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
This initially did work, but when I tried to modify the tables, there was a database version error and I could not use the database crud functionality with the program. I searched as to why this wouldn't work and looked through the error files and I found that once you change a .mdf file to v13.0 (from googling I believe MSSQLLocalDB is v13.0) it cannot be reverted to a v11.0 .mdf version. So I guess vs express 2012 can only use v11.0?
If anyone can help me or point me to somewhere where I can learn how to connect to this database I would be really grateful.

Related

ASP.NET Core MVC & Entity Framework not showing any of the tables in Visual Studio 2019

I need to see my tables in Visual Studio in an ASP.NET Core MVC application that I have. I'm actually following a tutorial from Mosh Hamedani about it and when I click on the .mdf file to open up Server Explorer and the Data Connections row nothing shows up under the DefaultConnection row.
This is strange because I'm able to add records to the database directly as the database objects show up from one session to the next after doing any CRUD operation on them. It looks like in the screenshot below.
From what I've browsed online, connection strings are normally the reason why a lot of these errors happen but I'm so new to ASP.NET Core MVC, and I have no way of knowing how to troubleshoot the connection string to solve this kind of issue. But I'm going to post it anyway here so that maybe someone willing to help will know what to do it with it.
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Vidly-20220707050258.mdf;Initial Catalog=aspnet-Vidly-20220707050258;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

In MVC Migration, I can't update-database from one solution but can do it from another

I created a new ASP.NET web application in VS2017 as blank and installed Entity Framework in it and all important libraries.
Problem is:
I successfully ran Enable-Migrations
I successfully ran Add-Migration Initialize
But I can't execute update-database because I can't connect to SQL Server Express just from this project
I tried to open other project and it fork very fine
I think there is a problem in the web.config file because I start from scratch
I add a connection string as follows:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Server=OJ-PC\OJ_SQLEXPRESS;Database=OrdersDb;User Id=sa;Password=******;"
providerName="System.Data.SqlClient" />
</connectionStrings>
I tried a lot of connection strings. I get connection string from SQL Server and add it exactly. I tried same connection string that worked in other project but it didn't work in this project.
In two days I tried everything
Thanks for help!
The . (dot) alias is used when you deal with SQL Server Professional edition.
When it comes to dealing with SQL Express the server name consists of 2 parts separated by backslash \.
For instance
DESKTOP-3PQ45JH\SQLEXPRESS
In this case DESKTOP-3PQ45JH is the name of computer, SQLEXPRESS is the name of server instance.

Adding EntityClient into Connection String SqlClient

I am trying to add a Entity Framework project with object relational mapping (ORM) into my standard SQL Server Project (MVC ASP.Net).
I think the problem is in the Connection String in the Web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Project2-20160209033141.mdf;Initial Catalog=aspnet-Project2-20160209033141;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="BlogEntities" connectionString="metadata=res://*/Entities.BlogContext.csdl|res://*/Entities.BlogContext.ssdl|res://*/Entities.BlogContext.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\aspnet-Project2-20160209033141.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
DefaultConnection is the main project im working with and BlogEntities is what i'm trying to add.
When i run the project the BlogEntities it seems to work fine, but im having trouble running the Default Database, as It wont let me log into user accounts and other tasks that have worked before.
Error Example->
Is there an easier way to add this into my project without disrupting the current database?
Thanks in advance.
Entity Framework User system uses the default connection, you cant use another connection to change anything related to users.

.mdf file is not created on ASP.Net MVC 4 app after adding <connectionStrings> tag on Web.config

I was trying the ASP.Net MVC 4 tutorial. When I have added the following code and build the application I was expecting Movies.mdf file will be created under App_Data folder on the Solution Explorer. However, nothing has been created in that directory. Any idea why?
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcMovie-2012213181139;Integrated Security=true"
providerName="System.Data.SqlClient"/>
<add name="MovieDBContext"
connectionString="Data Source= (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Using:
.NET Framework 4
VS 2010
Having a full instance of MS SQL 2012 on my machine, not SqlExpress, I had this problem. I found that by letting the application continue (instead of halting it in visual studio when VS catches the exception) I got a helpful message in the browser:
Directory lookup for the file "c:\users\chris\documents\visual studio
2012\Projects\MvcApplication1\MvcApplication1\App_Data\aspnet-MvcApplication1-20140225162244.mdf"
failed with the operating system error 5(Access is denied.). CREATE
DATABASE failed. Some file names listed could not be created. Check
related errors.
which tells that the problem is that the sql service doesn't have write permissions to my application directory.
Giving it permissions was slightly tricky because the username needed didn't come up (in Windows 8 Pro) in the Explorer GUI for Security. The username for a Default Instance of Sql Server is NT SERVICE\MSSQLSERVER (for an instance it's NT SERVICE\MSSQL$InstanceName) and I had to type it in by hand, and then give it write permissions. I did this just on the App_data directory.
Then it worked.
The problem is solved. The problem was the location of the project. I was working with the files from a shared drive which apparently did not create the database in the App_Data folder. Connectionstring code is absolutely fine as it is shown in the tutorial and no changes are required.
The database will not be created before you actually add the first record to one of your tables.
This error occurs when Visual studio and MSSQL are running under different authorities. For this reason, your application doesn't get to have a write permission for creating the .mdf database file.
SOLUTION
Open SQL Server Configuration Manager
Select SQL Server Services
See the Log On As tab
the value would be something like NT AUTHORITY\NETWORKSERVICE ( it depends )
Once you get the user name,
then go to the target folder. ( If default, App_Data in your project directory )
right click -> property windows -> security tab
click edit -> check the user or group name
You shall not see there's no Network Service on the user or group name list
All you have to do is to add the user name and give the write permission for that user or group.
It may be the case of AttachDBFilename... I have this for my project and it works fine.
... connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Movies.mdf" providerName="System.Data.SqlClient"
Also verify your model is correct. If your model does not initiate this connection then it will not create the database.
I have resolved the issue by modifying the connectionstring like below
<add name="MovieDBContext"
connectionString="Data Source=.;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
I did install Microsoft SQl Server Compact 4.0 from nuget.. but I am not sure whether it is really require to install it or not.
In this MVC tutorial, just add "Initial Catalog=Movies;" (without quote) to the connection string and it'll work well.
<add name="MovieDBContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>

Unable to find the requested .Net Framework Data Provider. It may not be installed

I've got an application running on ASP.NET MVC 3 with Entity Framework Code First. In development I was using a SQL Compact database, however upon moving this to my virtual server, I am attempting to target SQL Express.
There were initially issues to do with a "CREATE DATABASE in master" error, which I got around by extracting the model from the SQL Compact database into an SQL script and executing that on the server to create the DB.
I have created a new connection string to point at the SQL Express instance, which uses the EF format:
<add name="LouiseClarkEntities" connectionString="metadata=res://*/Models.LouiseClark.csdl|res://*/Models.LouiseClark.ssdl|res://*/Models.LouiseClark.msl;provider=System.Data.EntityClient;provider connection string="Data Source=.\SQLEXPRESS; Initial Catalog=LouiseClark; User ID=<username>; Password=<password>"" providerName="System.Data.EntityClient" />
The error I am now getting when navigating to a page that uses the DB, is:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
I have installed Entity Framework 4.1 on the server to try and see if this would solve the issue, but it didn't seem to do much good.
Snippet from the stack trace on error page:
[ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.]
System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName) +1420567
System.Data.EntityClient.EntityConnection.GetFactory(String providerString) +35
[ArgumentException: The specified store provider cannot be found in the configuration, or is not valid.]
Any help would be appreciated, as this has been bugging me for days now!
Thanks,
Chris
Use the normal connection string with code first.
<add name="LouiseClarkEntities" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=LouiseClark; User ID=<username>; Password=<password>" providerName="System.Data.SqlClient" />
On your second problem, and partly the cause of the first too...
what you did wrong is which I got around by extracting the model from the SQL Compact database into an SQL script and executing that on the server to create the DB.
You should use migration scripts for that - and use in PM console then Update-Database -Script to dump out what you need - then deploy that to the server Database.
Problem is that there are CF has its own table and data that needs to be initialized properly. If that doesn't match you'll end up with something like that.