Automatic generation of EF entities and mappings from existing database schema

This is  a bunch of useful scripts for generating C# Entity Framework Code First POCOs from existing database schema. First, let’s create T-SQL function for generating C# class for EF entity: This function deliberately ignores certain columns, (Created On/By, Modified On/By). This properties are pushed to EntityBase class, which our POCO inherits from. All other columns in DB table will be mapped to corresponding C# type. Next, let’s create T-SQL function for EF configuration (mapping class): Now, let’s put it all…

Read More

Git aliases I can’t live without

Being still quite new to git (and moving from world of VSS/TFS), I became big fan of Git’s flexibility and configuration. It really boosts productivity and makes developer’s life much easier. Below I list git aliases I found most useful. For sure this list will grow in future. alias.cam=commit -am alias.st=status alias.aa=add . alias.ba=branch alias.co=checkout alias.hist=log –oneline -10  

Read More

Unit Testing stack for C#/Visual Studio

After spending some time on digging through unit testing components, I ran into a setup I am finally happy with. This consists of: https://github.com/AutoFixture – acts as DI container/object factory, significantly improving unit test maintainability https://github.com/moq – awesome mocking framework, fully integrated with AutoFixture https://www.nuget.org/packages/SemanticComparison – excellent utility for comparing instances https://github.com/shouldly/shouldly – great for simplifying assertion statements https://github.com/xunit/xunit – superb unit testing framework, supporting parameterized tests, multiple test executions and much more Although each utility is great on it’s own, when combined together, this stack…

Read More

T-SQL Tricks

Few useful SQL tricks: Move table to different schema ALTER SCHEMA targetSchema TRANSFER sourceSchema.tableName Rename object sp_rename ‘schemaName.tableName.columnName’, ‘newColumnName’

Read More

Simplest way to restore MSSQL Server database

Here’s a simplest way I found (so far) to restore a database in MS SQL Server. First part lists “logicalnames” from the backup that you want to restore. Modify the path so it points to the actual path where your backup file is: RESTORE FILELISTONLY FROM DISK = N’C:\[path]\[db_name_backup].bak’ GO This shows LogicalName and PhysicalName of the data and log backup files, which have to be used in the following script: RESTORE DATABASE [db_name] FROM DISK = N’C:\[path]\[db_name_backup].bak’ WITH FILE…

Read More

How to delete all stored procedures from MSSQL database using cursor

Here is a code for deleting all stored procedures in SQL Server database using cursor. DECLARE @name varchar(500) DECLARE @sql varchar(max) SET @sql = ” DECLARE cur CURSOR FOR SELECT [name] FROM sys.procedures OPEN cur FETCH NEXT FROM cur INTO @name WHILE @@fetch_status = 0 BEGIN SET @sql = ‘DROP PROC ‘ + @name PRINT @sql EXEC (@sql) FETCH NEXT FROM cur INTO @name END CLOSE cur DEALLOCATE cur

Read More

Animated iPhone-style ImageButton with Silverlight 4

In this article I am presenting a way to create animated iPhone-style ImageButton control in Silverlight 4. Button will have icon on it, rounded corners and will zoom-in/zoom-out on hover. It will be done programmatically, with very minimum amount of XAML. I start with creating ImageButton class. I use ButtonBase as base class: I want my button to contain rounded rectangle with shadow and image on it. I add these items in constructor: Now my control contains rectangle and image…

Read More

Using Sets in .Net

Great post by James Michael Hare I came across about using Sets in .Net. Definitely worth taking a look. http://geekswithblogs.net/BlackRabbitCoder/archive/2011/02/03/c.net-little-wonders-the-useful-but-overlooked-sets.aspx

Read More

WCF Custom tool error: Failed to generate code for the service reference.

Very recently, building multi-tier application involving Silverlight front-end inter-operating with WCF back-end, I encountered very odd, yet annoying behaviour of Visual Studio 2010. I was building new functionality using Telerik RadMap for Silverlight control that was to display some spartial data supplied by WCF service. To achieve this my Silverlight application had to reference few Telerik assemblies (Telerik.Windows.Controls.DataVisualization.dll). What is important is fact, that Service Reference for WCF service had been there already, project built and everything was working nicely,…

Read More