migrate database from one MVC project to another - asp.net-mvc

I am using Visual Studio 2013. Unfortunately I am facing some problems with my MVC project. So I created a new project and I need to fetch database to my new project but can't figure out how to fetch database. I am using data first approach, entity framework and IIS Express localhost.
Update:
I copied connection string from web.config of old project and pasted it over new project's connection string.(Note I first created ado.net entity data model in new project and then pasted string over its connection string).
Now in server explorer window > data connections my Entities connection has changed to defaultconnection and I get exception The underlying provider failed on Open on executing query of new projects database. But I am unable to fetch old database tables.

Your DBContext likely has the name of the database connection in one of it's constructors.
This corresponds to the name of a connection string the web.config.
So move that configuration to your new web config and make sure your new DbContext references the correct name.

Related

MVC 5, ASP.net data is storing to mystery location

I created a mvc5 asp.net website that connects to sql server database. Looks like when ever I register a user through this website, user is created to database called DefaultConnection that is located at C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA.
But my config file is not pointing to this. My config file is pointing to a proper database at certain server. Also my connection has different name like
<connectionStrings><add name="TestEntities"....
And when I run my website it correctly bringing info from this database. Problem is only when I register a user.
The membership provider doesn't know to use a connect string named TestEntities, by default it uses a connection string named "DefaultConnection" and seemingly has a fallback in case that isn't present.
(See this to be more precise: ASP.NET Configuration File Hierarchy and Inheritance)
You could:
change your connection string name to DefaultConnection
copy your connection string and call the copy DefaultConnection
override the connection string name used by Entity Framework under the hood using the config below. I believe you can also override the connection string name used by creating you own DBContext.
Personally, I'd go with option 2, provides flexibility if you ever want to move the user tables out to a seperate database.

How can remove connectionstring from webconfig?

I have a project that has 3 layers.
1)Interface (It is an MVC project with View and Controller)
2)BI (It is a class library project)
3)DAL (It Is A class library that work with Entity Framework DataBase First)
My Problem :
There is a connection string in webconfig of the first project(MVC). But I want remove it. then the other layers should use of DAL Connection Strings.
But when I delete the connection string from that it cant connect and work with DB !!
I remember when I was working with ASP.NET and Linq to sql it was posible.
Please help me.
Once deployed, the only configuration that will be available will be the web.config of your MVC project. Instead of removing the connection string you could just encrypt it.
ASP.NET Connection String Encryption / Protection
Programmatically encrypting a config-file in .NET
http://msdn.microsoft.com/en-us/library/89211k9b.aspx
Personally I'm using a mechanism based on that blog post. I have an administrative api call on my web application to send the information that must be encrypted.
- http://weblogs.asp.net/sukumarraju/archive/2009/09/28/encrypt-and-decrypt-connectionstring-section-in-web-config.aspx

How do I connect to a code-first created localdb database using database explorer?

I have a database in an MVC 4 project that has so far been entirely managed via Code First and Migrations. I now want to go in and change some of the data by hand. How can I connect to the localdb instance using Database Explorer within Visual Studio 2012? The connection string is as follows:
Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-mydb-20120830192823;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-mydb-20120830192823.mdf
I have an App_Data folder in my project, but the .mdf named in the connection string is not in there.
I have tried connecting to '(LocalDb)', 'LocalDb', '.SQLEXPRESS' and various other things in the Add Connection dialog, but nothing seems to work.
Duh, it was '.\SQLEXPRESS' I needed to connect to.

LINQ to SQL cannot connect to database, but the connection string is good

I've deployed a MVC site and it's database to a web hosting provider. The default ASPNET tables and the applications custom ones share the same database. There are two connection strings in the web config, one for ApplicationServices and one for my LINQ to SQL dbml. As they share the same database then I use a identical connection string for both.
The application services functions seem to work as I can log into the site. However the return controller throws an exception because it's trying to use my data context this time. Identical connection string and yet it gives me the 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: Named Pipes
Provider, error: 40 - Could not open a
connection to SQL Server)
This of course all works properly on my local machine. I would very much appriciate any suggestions as to why LINQ to SQL can't connect with a valid connection string.
The LINQ to SQL dbml file is inside a class library and the startup ASP.NET web project consumes this library as it's data layer. LINQ to SQL was reading it's connection string from the app.config of the class library when run through visual studio. When publishing the web project the dependent data library gets included but not it's app.config. I understand now that the fully qualified connection strings need to be manually copied into the web.config of the primary project where they override the defaults for the assembly. The problem I was facing was that with the absence of an app.config and no connection strings in the web.config, at least not fully qualified ones, the library was falling back to it's development defaults.

How do i get my Windows Azure Worker Role to connect to my Ado.NEt Entity-based db

I've built a backend in ASP.NET MVC2 which has an underlying ADO.NET Entity-based Database.
In the MVC Backend, I call my database entities, i.e.:
Entities entities = new Entities();
...and that all works fine.
Unforutnetly, in my Azure/mvc2 project, My worker role makes the azure project throw weird exceptions:
"The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid."
Any ideas ladies and gentlemen?
Solved it myself - it was a PEBKAC issue.
Had to manually set the configuration string in the Worker role's App.config (Copypasted from from the ASP/NET MVC2 Project's Web.config).
All works well now.

Resources