Code First Migrations, Database Projects, and SQL Azure - ef-code-first

We are in the process of developing a new site that we plan to host on Azure. None of us have done this before, so it has raised some interesting challenges.
First, we are using code first and using migrations to update the database. We were having some issues figuring out how to deploy the database to Azure, so we ended up creating a database project.
There are a bunch of Azure specific issues with deployment, and you will get errors if you don't rectify all the valid SQL that Azure doesn't allow you to use.
It seemed to me that you should be able to do code first on the azure site just like we did on the local site, and I found This article that seems to address the issue.
What I am wondering though, is best practices.
I would assume we don't need database projects anymore, but I am not
sure about that. What is the best practice for managing databases
with Visual Studio 2013 and code first.
The approach in the article above seems fine for a system in
pre-production, but once a system is in production and has live
data, is this a recommended approach?
How do you protect yourself from mistakes like somebody accidentally changing a
setting that tells Visual Studio to recreate the database?
Any thoughts or ideas would be appreciated.

First i would like to state from my understanding, when using Entity Framework and you have Enabled Migration, it's only meant for development and it is recommended that you set it to false when deploying your code.
As for deploying to Microsoft Azure, i have only done this using Visual Studio Team Services. The thing about Code-First is that you have no Sql Queries and the database is not deployed right away. So here is what i have done in the past,
Create a blank Sql Query in SQL Management Studio
In Visual Studio Server Explorer expand your database for the
project to table level
Right click on each table and select "Show Table Definition" I wish
we could do this at Database level.
Copy and past the T-SQL to a blank Sql file in SQL Management
Studio. Note that all table queries can fit in one
file, just have to follow T-SQL rules. Connect to your Azure
Database Instance using SQL Management Studio.
Run the SQL Query against the database on Azure. Note that the
sample data you had used in developemnt will not be on your Azure
database at this moment, you will have to create an insert T-SQL and
run it against the Azure database.
Also make sure that your app has the right connection string when deploying to Microsoft Azure and it should connect to you database just fine.

Related

Using Azure SQL for asp.net

I've designed a website in Visual Studio and I'm starting on the database aspect of the site now. I'm using Azure to host the site at the moment and I was wondering is it best to create the database on Azure and then connect it to asp.net or design it in Visual Studio and then connect to Azure so I can store it there?
I also run the website locally a lot when I am testing it for design purposes so would building it in Azure affect this?
Im also using visual studio ultimate 2013 but its a pre release version and I can't use the azure sdk with it. Will it work on the full version of 2013?
Azure SQL Database at its core is essentially compatible with standard SQL Server. There is a published guide on what the limitations are (http://msdn.microsoft.com/en-us/library/windowsazure/ee336245.aspx) that you should understand first but I'd recommend that you work locally and then push your changes to Azure SQL Database. Note that like any changes to schemas / databases in SQL Server you'll still need to manage those transitions appropriately as part of your deployment process. HTH.

How can I implement a local testing database for my Azure Website?

I have a website I'm developing that uses an SQL Azure database. I want to be able to test my website using a database locally hosted on the debugging machine so that my designer can work on style and content without having to deal with the overhead of firing up the Azure emulator and communicating with an external service as he is often in areas with no-connectivity.
I imagine it would be possible to extract the .sql scripts to create the database and execute them every test run, but this seems tedious. Similarly I'm not sure what the best way to configure these deployment details would be in order to switch between development, and published configurations. I'm a new web-developer cutting my teeth on a rather large project.
Using ASP.NET MVC4 and have MSSQL 2012 installed for what it's worth.
You can export your SQL azure database as .bacpac format and then import it into your sql server database. It will create all tables and fill with data. You don't need to do it on every test run, just do it once and you will have proper database for debug needs.
Switching between debug and release (or you can rename it if you want, e.g. Local and Production) configurations and using different web.config (or config transformations) is a good way to work with different settings.
If you want to store your db scripts (db structure or data) in your VCS - you can use Entity Framework migrations (new fancy way) or separate project of "SQL Server database" type (old school but proven way :) ).
Also EF migrations allows you to easily recreate db (with different name) on each run (for unit testing purposes. And then you can use SQL Express file instance (keep in mind that it's only for local work, your designer wont be able to access sql express instances afaik)

Which database to use for C# ASP.NET website?

I am developing an online internal web portal (like users write tests, provide document links to study, completing a study roadmap, admin user to view ststistics, etc). It caters some 150 users. I have planned to use ASP .NET with c# in Visual Studio 2008 on windows xp. I am implementing using Forms and NOT MVC since I feel at home with forms.
I do not want to use windows authentication since each user will have many attributes related to him and so it will be easy using a database. Security is not an issue since it is being used internally only.
I have only a fair knowledge about ASP and C# and VS 2008.
Now my questions:
Can I use MS SQL server database that is built in in VS 2008?( I cannot ask for external databases)
Can I export the website totally along with the databases to IIS server running in some other computer? How?
Do I need to export databases separately or provide a database creation script like PHP?
(I have more questions. Will update once I start off).
(This is my first .net web app. so can i know where i can find login scripts, pagination, examples, and pretty much all the stuff)
edit: which to use? New website or New Webapplication?
You can pretty much use any database, but it seems from your requirements that you want to look closer towards something like SQLExpress, SQL Server Compact Edition or SQLLite.
Can I export the website totally along
with the databases to IIS server
running in some other computer? How?
Your database will be shipped along with your application if you use one of the database options that I have specified above.
Do I need to export databases
separately or provide a database
creation script like PHP?
See my comment above, the database will exist when you deploy your website to IIS. It's a physical file that will be in your APP_DATA folder.
You can use:
MySQL. The reason: It has no space or memory limit!
SQL Server or SQL CE. These are both alike. They just differ in slite functions, and also the storage!
The SQL CE allows you to have 4GB. But the SQL Server lets you have 10GB.
So it depends on your work!
Also, I wanted to point out one error in a post, that the SQL CE is present in App_Data, Correct! But the SQL Server is placed somewhere like:
C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\here_will_it_be
And yes, you can migrate it to wherever you want to place it. I mean if you want to upload it to a hosting server, you can upload it using Web Deploy! Or what ever service. When downloading you will have to repeat same process!
I hope you are doing good with your first web app! :)

How to automatically check out a database file in a source controlled web application?

I am working on an ASP.NET web application, we are a small team (4 students) and we do not have access to a dedicated server to host the database instance. So for this web application we decided just to put the database file in the App_Data folder.
The problem is that our project is source controled on TFS, so every time you open the solution and try to launch the web application, we get an expcetion saying that database is read-only. That is logical because the databse file is not automatically checked-out.
Is there a workaround to avoid a manual check-out of the database file everytime we open the solution ?
Thanks.
Never check in MDF files. You should develop your project with a deployment script that creates the tables and other database objects you need. Never modify the MDF directly from VS Server Explorer. Use a version based approach to database schema.
I know you'll say 'but is hard', but trust me, the alternative which VS wizards guide you is far worse. Not only from a team development point of view, but from deployment point of view too. After you deploy v1 of you site, you'll make changes and want to deploy v1.1 or v2. Each time, you'll face the same dilemma: you have 4 MDF files, one on each team member enlistment, and 1 on the deployed site, and there is simply no way to align the schemas. You'll end up asking for SQL diff tools like SQL Compare, but they cost a tonne of moneys and they're far from perfect when it comes to maintaining a SQL schema.
Besides, having scripts as your database source will also align well with all the other source control goodies, like tracking down who changed what and when.
If you are able to install "Visual Studio Team System Database Edition" add-on for Visual Studio (previously called "Visual Studio Team Edition for Database Professionals", previously called "DBPro") you can import your current database into a Visual Studio Database project, which can be part of your overall VS Solution.
This then allows you to manage schema objects (tables, stored procs, permissions etc.) in Visual Studio and integrates your database schema nicely with TFS.
It also allows you to deploy the schema in your VS Project to the database server, automatically handling modifications without causing data loss (or failing if data would be lost).
The add-on for VS 2008 is located at:
http://www.microsoft.com/downloads/details.aspx?FamilyID=bb3ad767-5f69-4db9-b1c9-8f55759846ed&displaylang=en

SQL server express backend for ASP.NET web service

I come from a linux/apache/php/mysql background. For my current project, I am forced to write web services using ASP.NET. I have installed visual studio and created an ASP.NET web service project. The web service I'm creating will use a SQL database backend. I see that visual studio installed "SQL Server Express 2008." I can see that there is a service called "SQL Server (SQLEXPRESS)" that is running. My question is, how do I run queries against this database? Where's the front end? What tool do I use to create databases and tables? Is there something else I have to install?
I feel completely lost and my google-fu fails me. There are so many different SQL products from microsoft that I can't tell what is the easiest path to just having a simple database with tables I can query from my development machine. With MySQL, I would install the MySQL database, download MySQL query browser and start firing queries at it. How can I do that with Visual Studio/SQL Server Express?
You can download SQL Server Management Studio from here. This is the GUI for working with SQL Server. It will let you create databases, tables, stored procedures, etc. as well as writing queries and manually edit data in the tables.
Aside from SQL Server Management Studio, you can use the Server Explorer tab in Visual Studio (by default on the right side, next to Solution Explorer) to add a data connection. You can do most of the most common database tasks (create/query tables, create views, etc) from there.
there's a command line tool that installs with SQL Server (all versions) called SQLCMD.exe, should be in your install path.
Visual Studio also lets you create dbs, run queries, look at table structures and data (including creating and deleting them, etc), you do that by opening your Server explorer window and making a connection to your local instance (.\SQLExpress by default).
Finally, as the other person mentioned, SQL Server Management Studio Express is a free download, and well worth the time it takes to install.

Resources