Visual Studio Code Snippets

Visual Studio Code snippets are handy way of increasing your speed of development.

User keyboard shortcuts
CTRL+K + X for code snippet
CTRL+K + S for Surround With snippet

Here are most of the described: http://msdn.microsoft.com/en-us/library/z41h7fat%28v=vs.100%29.aspx

Or you can download this Word document and print them out – the same information, only in printer-friendly view.

StackOverflow as a default search engine in Chrome

This post is influenced by this discussion on Meta Stack.

In Chrome, go to Settings -> Manage Search Engine. There add a new search engine:
Call it StackOverflow.com, give it keyword so and put this as a search url:  https://www.google.co.uk/search?q=site:stackoverflow.com+ %s.

Then add this as a default, but still make google your default engine. When opening new tab, in url type so, hit <tab>, and he-ho! enjoy!

SQL Query to do multiple sums

Sometimes you need to do a report with totals column and with totals that depend on some condition, like
Product, Total Sales this year, total sales this month.
Use SUM(Case…) condition, like here:

SELECT  SUM(CASE WHEN order_date >= '01/01/09' THEN quantity ELSE 0 END) AS items_sold_since_date,
        SUM(quantity) AS items_sold_total,
        product_ID
FROM    Sales
GROUP BY product_ID

Credit goes to Joel Coehoorn

Another way to do Sum:

  COUNT(distinct case when Status.Complete = 1 then Status.ID else NULL end) as [Count Of Complete Status],

Alternative to cursor in SQL Server

You can use temp table in the same manner as you use SQL Server cursor

create table #temp (
    value1 int,
    value2 int
)

Declare 
    @SomeValue1 int,
    @SomeValue2 int

insert into #temp (
    value1, 
    value2)
select 
    field1, 
    field2
from realTable
--where some condition

select top 1 
    @SomeValue1 = value1, 
    @SomeValue2 = value2
from #temp

-- while we have rows in the table, do work with them.
while @SomeValue1 is not null 
Begin

    -- Do something with value1 and value2


    -- delete the row we have just worked with
    delete from #temp where value1=@SomeValue1 and value2=@SomeValue2

    -- don't forget this bit!! otherwise you'd be stuck in the infinite loop, 
    -- as variable is not reset to null on empty table
    set @SomeValue1 = null  

    -- get the next row  in the table
    select top 1 @SomeValue1=value1, @SomeValue2 = value2
    from #temp

End

-- here we are done, can drop the temp table
drop table #temp

Probably you would benefit from adding @ID uniqueidentifier to temp table, in case you have repeating data, and delete rows from temp table based by the ID value

EF 4.3 Database Migration Commands Cheatsheet

Code-Based Migrations

Original: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx

Enable-Migrations
Add-Migration – scaffold next migration based on changes made to the model.
Update-Database – update database model to the latest migration model
Update-Database -Verbose – update database model and output SQL generated
Update-Database -TargetMigration: “someMigration” – update database up (or down) to a “revision”
Update-Database -Script -SourceMigration:$InitialDatabase -TargetMigration:”AddPostAbstract” Generate SQL script of the migrations
SQL(“update sometable set somefield=’hi'”) – can insert custom SQL into migration class.

Automatic Migrations

Original here: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx

Enable-Migrations -EnableAutomaticMigrations -Force – turn on automatic migrations

Basically does the same as code-based migrations, but before doing Database-update it automatically calls Add-Migration and gives it a name.

When you use Data Definition Language (DDL) to modify a table, and then you try to save the table in Microsoft SQL Server 2008, you may receive the following message:

Saving changes is not permitted. The changes that you have made require the following tables to be dropped and re-created. You have either made changes to a table that can’t be re-created or enabled the option Prevent saving changes that require the table to be re-created.

To change the Prevent saving changes that require the table re-creation option, follow these steps:

  1. Open SQL Server Management Studio (SSMS).
  2. On the Tools menu, click Options.
  3. In the navigation pane of the Options window, click Designers.
  4. Select or clear the Prevent saving changes that require the table re-creation check box,  and then click OK.

Source: http://support.microsoft.com/kb/956176