Posts

Showing posts from 2012

“Entity Framework Reverse POCO” OSS project made top link today (Dec 7)

Image
I saw a spike on the Entity Framework open-source project I created, and had a look around to see what it could be…   Found it listed as the top link on http://www.alvinashcraft.com As developers we take so much (understatement) from the open-source community ourselves, it’s nice to give back from time to time. I've also noticed that it is now listed in the "most popular" category in the VisualStudio add new item.

EntityFramework Reverse POCO Code First Generator

Reverse engineers an existing database and generates EntityFramework Code First POCO classes, Configuration mappings and DbContext. To install and use this project: Use Nuget and install EntityFramework. Add a connect string to your app.config. Somethine like: In Visual Studio, right click project and select "add - new item". Select Online, and search for "reverse poco". Or you can download it from this page. Select "EntityFramework Reverse POCO Code First Generator". Give the file a name, such as Database.tt and click Add. Edit the Database.tt file and specify the connection string as MyDbContext which matches your name in app.config. Save the Database.tt file, which will now generate the Database.cs file. To see a video, head over to http://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838

Obtaining the database schema, tables, columns, and primary keys in a single SQL call

I am currently writing an EntityFramework reverse engineer code-first generator. It will generate POCO classes, DbContext and Code First mapping for an existing database. There is one already in Entity Framework Power Tools Beta 2 However this one is going to do the job right, and include table filtering! Watch this space... SELECT [Extent1].[SchemaName], [Extent1].[Name] AS TableName, [Extent1].[TABLE_TYPE] AS TableType, [UnionAll1].[Ordinal], [UnionAll1].[Name] AS ColumnName, [UnionAll1].[IsNullable], [UnionAll1].[TypeName], ISNULL([UnionAll1].[MaxLength],0) AS MaxLength, ISNULL([UnionAll1].[Precision], 0) AS Precision, ISNULL([UnionAll1].[Default], '') AS [Default], ISNULL([UnionAll1].[DateTimePrecision], '') AS [DateTimePrecision], ISNULL([UnionAll1].[Scale], 0) AS Scale, [UnionAll1].[IsIdentity], [UnionAll1].[IsStoreGenerated], CASE WHEN ([Project5...

FizzBuzz generators

SQL WITH mil AS ( SELECT TOP 1000000 ROW_NUMBER() OVER ( ORDER BY c.column_id ) [n] FROM master.sys.all_columns as c CROSS JOIN master.sys.all_columns as c2 ) SELECT CASE WHEN n % 3 = 0 THEN CASE WHEN n % 5 = 0 THEN 'FizzBuzz' ELSE 'Fizz' END WHEN n % 5 = 0 THEN 'Buzz' ELSE CAST(n AS char(6)) END + CHAR(13) FROM mil C# foreach (int number in Enumerable.Range(1, 100)) { bool isDivisibleBy3 = (number % 3) == 0; bool isDivisibleBy5 = (number % 5) == 0; if (isDivisibleBy3) Console.Write("Fizz"); if (isDivisibleBy5) Console.Write("Buzz"); if (!isDivisibleBy3 && !isDivisibleBy5) Console.Write(number); Console.WriteLine(); } C# linq Enumerable .Range(1, 100) .Select(i => i % 15 == 0 ? "FizzBuzz" : i % 5 == 0 ? "Buzz" : i % 3 == 0 ? "Fizz" : i.ToString()) .ToList() .For...

SQL Lotter number selector

Just a bit of fun with SQL WITH L0 AS (SELECT 0 AS C UNION ALL SELECT 0), L1 AS (SELECT 0 AS C FROM L0 AS A CROSS JOIN L0 AS B), L2 AS (SELECT 0 AS C FROM L1 AS A CROSS JOIN L1 AS B), L3 AS (SELECT 0 AS C FROM L2 AS A CROSS JOIN L2 AS B), Nums AS (SELECT TOP(49) ROW_NUMBER() OVER(ORDER BY (SELECT 0)) AS n FROM L3 ORDER BY n), Choice AS (SELECT TOP(6) n FROM Nums ORDER BY CHECKSUM(NEWID())) SELECT STUFF( (SELECT ',' + CAST(n AS VARCHAR(10)) AS [text()] FROM Choice ORDER BY n FOR XML PATH('')), 1, 1, '')

Effortless .Net Encryption

I've just released a new open source project called Effortless .Net Encryption. It can be found here: https://github.com/sjh37/Effortless-.Net-Encryption Effortless .Net Encryption is a library that is written in C# 4.0, contains 68 unit tests and 190 Pex unit tests, and provides: Rijndael encryption/decryption. Hashing and Digest creation/validation. Password and salt creation. Available on Nuget https://nuget.org/packages/Effortless.Net.Encryption/ To install Effortless.Net.Encryption, run the following command in the Package Manager Console Install-Package Effortless.Net.Encryption

A generic singleton using Lazy< T >

Carring on from the generic singleton post here. Since we have .NET 4, we can now make use of Lazy public class Singleton where T : class, new() { private Singleton() {} private static readonly Lazy instance = new Lazy (() => new T()); public static T Instance { get { return instance.Value; } } }

Please turn off hyperlinks in PowerPoint

Image
When giving presentations, you should really turn off PowerPoints AutoCorrect feature of it changing a hyperlink into an underlined link. Unless you actually are going to click on the link during the presentation, then turn it off. It's much easer to read without it being in a different font and underlined. To turn it off in PowerPoint, goto Tools -> Options -> Proofing -> AutoCorrect options. Untick the box highlighted below.

Recursive CTE (Common Table Expression)

There is sometimes a problem of wanting to remove data (email addresses in this example) from within a string which are delimited. For example, if you want to remove all non bybox email addresses from "some.name@bybox.com; simon@hicrest.net; fred.bloggs@bybox.com" and do this for every table, without having to create functions to break apart the string first, how are you going to do it? Here's how: Our example table looks like this CREATE TABLE data_export ( data_export_id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY, email_address VARCHAR(255) NOT NULL -- ... Other fields left out for brevity ) Insert some test data INSERT INTO data_export (email_address) VALUES ('some.name@bybox.com; simon@hicrest.net; fred.bloggs@bybox.com'), ('neo@matrix.com; me@bybox.com'), ('fred@b.com; xxx@bybox.com'), ('fred@bbc.com'), ('an.other@bybox.com') Next is the recursive CTE SQL is in several sections: spli...

C# 5 attributes on optional parameters

With C# 5, you can put a special attribute on an optional parameter and the compiler will fill in the value not with a constant but with information about the calling method. This means we can implement the Logger.Trace to automagically pick up where it’s being called from: public static void Trace(string message, [CallerFilePath] string sourceFile = "", [CallerMemberName] string memberName = "") { string msg = String.Format("{0}: {1}.{2}: {3}", DateTime.Now.ToString("yyyy-mm-dd HH:MM:ss"), Path.GetFileNameWithoutExtension(sourceFile), memberName, message); LoggingInfrastructure.Log(msg); } Now, if the caller calls Log.Trace("some message") the compiler will fill in the missing arguments not with the empty string, but with the file and member where the call happens: // In file called Fred.cs public void SomeFunc() { Log.Trace("Hello"); // Compi...

www.stilettos-sos.com

Image
I've created yet another WordPress CMS system for a friend of my wifes this time. Home page is a blog, the others are static pages. www.stilettos-sos.com

The real doomsday date is Tue Jan 19 2038 at 03:14:07

Some said it was the Y2K bug we had to worry about, but it's actually Tue Jan 19 2038 03:14:07 you've really to worry about. There are many, many systems built with time saved as a 32bit long integer value. The max value of a signed long integer is 2147483647. This value is the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC) Here is an example C program, built with Visual Studio 2010. __time32_t t; t = 0; printf( "The min is %s\n", _ctime32( &t ) ); t = 2147483647; printf( "The max is %s\n", _ctime32( &t) ); t = 2147483648; printf( "The date is %s\n", _ctime32( &t) ); Output: The min is Thu Jan 01 00:00:00 1970 The max is Tue Jan 19 03:14:07 2038 The date is (null) So after Tue Jan 19 03:14:07 2038, the date goes to null which will cause a crash. Let's just hope all the critical (including embedded) systems get fixed before then.

PowerShell script to automatically clone/update all repos in Kiln

Image
PowerShell Script files Download the files here . Change and replace the following url Change https://your fogbugz root url to be your root url for fogbugz. For example, if your companies fogbugz url is something like https://secure.bbc.com/FogBugz/default.asp, then your root url will be https://secure.bbc.com Obtaining a Kiln Token The first thing you need to do before you can use this script is to obtain a Kiln token. Copy and alter the following link in your browsers URL https://your fogbugz root url/FogBugz/api.asp?cmd=logon&email=[yourEmail]&password=[yourpassword] Changing the text in square brackets (and also removing the brackets). You should receive something like this: ymjt123f8882a6s7td0j8eefa6u2g8 Take the token text: ymjt123f8882a6s7td0j8eefa6u2g8 and paste it into the powerShell script (edit your local copy instead of the SVN one) Running the script Start PowerShell and run the script passing in the root path of your Kiln repos. Will it clone every...

Ecommerce site

Image
I forgot to blog about an ecommerce site I created for an artist: Shop I used Zen Cart and make my own custom template to match the main home site: www.lookonthebrightside.co.uk Website E-Commerce site

www.lizigns.com

Image
Just finished a new website for a friend called lizigns.com I played with three CMS systems: Joomla , concrete5 and finally settled on WordPress CMS system. I'm pleased with the result. It looks fresh and has all the meta tags Liz wanted. Plus is quick and snappy to use. My views on the CMS systems I tried: Jooma has good customisation of templates, and a very good admin side of things, but its actually quite slow in use. Concrete5 is excellent in everyway, apart from one major stumble. You can't modify any of the template colours in the admin panel. I couldn't find the right template I wanted, so I ditched it. WordPress . Good admin panel, so I was confident Liz could use it to edit her pages after I handed it over. And it has lots and lots of free template and plugins (like a gallery) to choose from.