mvc ef code first database creation map - asp.net-mvc

was wondering if it's possible to point the visual web express to a specific folder when it builds the database using entity framework code first. this question stemmed from this one:
mvc connection string code first
anyway, i was able to successfully build the dbase using code first but sql server management studio is looking at a diff folder. is it possible to piont the web express to that folder when creating the database or is it better to just point the sql server management studio to where it's building it to? (does that make sense?) i tried fiddling with the ssms properties but it still won't look at that folder..

It whole depends on the connection string. If you are using default connection string for web application it always creates database in App_Data folder - it is specified by AttachDbFilename=|DataDirectory|DatabaseFileName.mdf. Using this is recommended way if you want your web app to create database because it should have necessary privileges by default.
If you want to place the database "elsewhere" you should not use attached db file and instead let SQL server create database in its default location by calling omitting AttachDbFilename part of the connection string. This can require additional security configuration to allow web application to create the database.

Related

Change the Default Target Server for Entity Framework Code First Approach?

I want to apply my code first migrations directly to SQL server or Azure Database instead of using the Default .sql server or Localdb/mssqllocaldb.. is there a way to do this from web.config and without editing code in context class?

How to Link SQL Server Express to MVC

Aimed to link my pages to an existing db from SQL Server Express. There are only one table is gonna select. Followed NerdDinner tutorial, using LINQ-to-SQL designer to do this.
However the result does not overcome what it suppose, my table Dinner can not be called directly, what would be the mistake I make?
The result return *.dbml and NerdDinner with *.edmx. What are their different?
You must set your connection string to store database in sql server express.Note that the *.edmx database type will store in local file system maybe at App-Data folder in your project as a database object. but another type, say, *.dbml is for using at DBMS system ,here SQL server Express. Also you can convert the *.edmx type to *.dbml type via SQL server tool.
Look at your web.config file at root of project.At the configurations, you will see that the connection string is setted to store at local file system of type .edmx. try to change it.

Entity Framework creating settings table that I can't put on Azure

I've been trying to create an ASP.net - MVC app that uses entity framework to connect to an Azure database.
I used a database first method to try and create the Model, I was able to connect to my azure database and successfully create the model.
Everything was working perfectly and I was able to retrieve my values from my azure database then I tried to publish the website to an Azure website. The website works fine and I have the website registered as able to use the azure database. When I click on the button that hits the controller to access the database I then get "Sorry, an error occurred while processing your request."
When I looked into this a bit more I seen that when I created my Entity Data model using the wizard it created a connection string to a local db for my entity connection settings. I'm guessing this is saving some entity framework settings but my problem is obviously when I publish the website to Azure it can't access this local db.
Any suggestions on how to either get this DB onto Azure or avoid using this extra database?
Thanks
This is the best tool to migrate your local database to Azure. Before publishing your site change the web.config to point to the Azure database. It will solve your problem.
Like you already explained, it seems that you web.config still points to your local database. Try to look at your web.config and see if you have connection strings pointing to .\SQLExpress, localhost\SQLExpress, ".", ...
Then, like Geenthanga's explains, you need to modify this value before deploying (it should point to your SQL Azure database instead). But you shouldn't do this manually! Instead, consider using web.config transformations, as explained here: Web Deployment: Web.Config Transformation.
This way you can have 2 web.config transformation files:
Web.Debug.config: This file would contain the transformations to add the connection string pointing to your local database.
Web.Release.config: This file would contain the transformations to add the connection string pointing to SQL Azure.

Migrating .NET Database to Shared Hosting

Built an app locally with an EF code-first database - not sure how to upload it to a shared hosting environment such as GoDaddy. It makes sense that something would be amiss because on the shared hosting your code can't just go create a database, but on the flip side I can't find anything to copy the CREATE sql and create it on the server like you would with MySQL
Feel a little silly because I've been using .NET for over a year now but at work the databases are already set up and we have full control over our environments.
If the database has no data that you need to preserve the easiest method is just to install the app on the new host and set the connection string to your new database on the host. On the first attempt to load a page accessing the database, the database will automatically be created (note that you need to load a page which hits the database - sometimes the home page is not sufficient).
This method is a lot more straightforward than generating SQL and then executing it on the production database.
If there is data that you need to preserve then the best method will be taking a backup and installing the backup on the host. In SSMS simply right-click the database in the left pane, then Tools > Backup... To restore on the server connect to the server in SSMS and right-click the 'Database' node in the left panel and select 'Restore Database...' I'm not sure if the host provides a direct connection from SSMS but they should at a minimum have a mechanism to restore a .bak file.
Going forward you should ensure that you can execute SQL on your database as a very convenient method for deploying EF Migrations is to generate the SQL update script on the development server and then deploy this by executing it in production.
Depending on your web host, you may be able to restore the database. If this is an option, simply back up your database on your local machine and restore it on the server via the management console.
You can back up your local database using SQL Server Management Console. This works well even for larger databases as you can directly restore all your data, your schema, etc.
I've had experience with three different hosts so far and all of them have this as an option. You'll usually find this under the Database tab for the web site. The rest from there is up to you because it's usually different across the various hosts.

MVC 3 Adding a local Database

I am currently going through the MvcMusicSore tutorial (MVC 3). I have full sql server 2008 installed and created a local database by running the SqL scripts included in the MvcMusicStore-Assets data folder. However when trying to add the mdf to the AppFolder in Visual Studio 2010 I get the error Access Denied. I am completely stuck at this point and would appreciate any help to resolve this.
Most probably the mdf file is locked by some other process, not allowing the application to read it. If you mounted the database on SQL Server you need to use a connection string with the machine name instead of specifying the mdf file directly.
You can't copy or modify a live working database. And I don't see why you should.
You need connecting to it? Pick a way. LINQ to SQL, Entity Framework, NHibernate, ADO.NET...
If you really want to copy the database file for some reason, you must first stop the MSSQL Service (or detach the database), then do that.
Like others have said, you shouldn't need to add the actual .mdf into your project. If you have it running on your local SQL Server instance, you should be able to add it via Visual Studio's Server Explorer (plus that gets you your connection string).

Resources