simon-holman.net

.NET Developer and Web Hosting Guy

Follow me on TwitterRSS Feeds

  • Home
  • About
  • Sitemap

Changing the product key in Windows Server 2012

Oct 10th

Posted by Simon Holman in Windows Server 2012

No comments

We have starting provisioning Windows Server 2012 boxes for us to use internally and for our clients. One of the things that stumped me very early on was that the “Change your product Key” link was missing from the product activation page. I’m not sure why they removed it, but to change the product key now, you can open a powershell prompt and type

slmgr -ipk XXXX-XXXX-XXXX-XXXX-XXXXX

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit
  • Email
  • Tumblr
  • Pinterest
  • StumbleUpon
  • Digg
Powershell, Windows Server 2012
Login or Register with Reddit

Signing up for Reddit

Sep 26th

Posted by Simon Holman in General

No comments

Well I just attempted to sign up for Reddit and had an interesting experience to say the least.

Step 1, Click on the Login Or Register link, and the top of the popup page tells me

Login or Register with Reddit

Ok, I thought that’s what I was trying to do!

Step 2, Select a username. This seems harder than you might think as you can see below.

Selecting a username on Reddit

Step 3, fill in Capture and Click Create Account, That’s all there is to it right? Apparently no

Creating an account with Reddit

I finally got in (after closing the register box and trying it again a few times) with the username IsThisRedditUsernameTaken, apparently it was not!

I think these guys need to take a little look at their user experience.

 

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit
  • Email
  • Tumblr
  • Pinterest
  • StumbleUpon
  • Digg
Ajax Fail, Bad UX, Reddit

Adding a lookup field to an existing table using Entity Framework code first migrations

Aug 6th

Posted by Simon Holman in Entity Framework

No comments

As you build out a site using the code first approach, at some point you will most likely need to add an additional lookup field to a class. There are a couple of steps required above what’s automatically done for you by EF code first migrations to make things work happily.

If you consider the following simple class

class Client
    {
        public int ID { get; set; }
        public string ClientName { get; set; }
        public string ClientAddress { get; set; }
    }

If you add a migration at this point you’ll end up with something like

public partial class InitialSetup : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "Clients",
                c => new
                    {
                        ID = c.Int(nullable: false, identity: true),
                        ClientName = c.String(),
                        ClientAddress = c.String(),
                    })
                .PrimaryKey(t => t.ID);

        }

        public override void Down()
        {
            DropTable("Clients");
        }
    }

Once this class has been added to the database and you have existing data, adding another non nullable lookup field can be tricky. When you add your lookup and it tries to add the lookupID field to the existing Clients table, it’ll fail because the existing data will fail referential integrity due to their being no data in the lookup table.

Once we added a lookup named ClientStatus to the model, it would look like

class Client
    {
        public int ID { get; set; }
        public string ClientName { get; set; }
        public string ClientAddress { get; set; }
        public int ClientStatusID { get; set; }

        public virtual ClientStatus ClientStatus { get; set; }
    }

    class ClientStatus
    {
        public int ClientStatusID { get; set; }
        public int ClientStatusText { get; set; }
    }

and our migration code will look like

public override void Up()
        {
            CreateTable(
                "ClientStatus",
                c => new
                    {
                        ClientStatusID = c.Int(nullable: false, identity: true),
                        ClientStatusText = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.ClientStatusID);

            AddColumn("Clients", "ClientStatusID", c => c.Int(nullable: false));
            AddForeignKey("Clients", "ClientStatusID", "ClientStatus", "ClientStatusID", cascadeDelete: true);
            CreateIndex("Clients", "ClientStatusID");
        }

        public override void Down()
        {
            DropIndex("Clients", new[] { "ClientStatusID" });
            DropForeignKey("Clients", "ClientStatusID", "ClientStatus");
            DropColumn("Clients", "ClientStatusID");
            DropTable("ClientStatus");
        }

The problem lies between the CreateTable and the AddColumn commands. The lookup table will be created, but the ClientStatusID non nullable field can’t be added to the Clients table as there are no records on the ClientStatus table.

To get around this in my code, I am tweaking the Up function to insert a default row into the lookup table and assigning the value of 1 to the default value of the ClientStatusID field in the Clients table.

I’m adding

Sql("INSERT INTO ClientStatus (ClientStatusName) VALUES ('Active')");

and updating

AddColumn("Clients", "ClientStatusID", c => c.Int(nullable: false));

to be

AddColumn("Clients", "ClientStatusID", c => c.Int(nullable: false, defaultValue:1));

As we are just creating the ClientStatus Table, we can reasonably assume that the first record created will have an CLientStatusID of 1.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit
  • Email
  • Tumblr
  • Pinterest
  • StumbleUpon
  • Digg
ASP.NET MVC, EF Code First, Entity Framework, Migrations
12345»...Last »
  • Amazon Books

  • Popular Posts

    • Show and Hide a div using jQuery
    • Review of HTC Mozart with Windows Phone 7
    • Using Data annotations with partial classes and LINQ to SQL
    • Getting random records from a table using LINQ to SQL
    • Creating a Tag Cloud with c# and IEnumberable<>
  • Categories

    • ADNUG (2)
    • ASP.NET (3)
    • ASP.NET MVC (7)
    • DotNetNuke (2)
    • Entity Framework (1)
    • Expeed (1)
    • General (1)
    • Reviews (1)
    • SEO (1)
    • Visual Studio 2010 (1)
    • Web Hosting (2)
    • Windows Azure (1)
    • Windows Server 2012 (1)
  • Tags

    ADNUG Ajax Ajax Fail ASP.NET ASP.NET MVC Bad UX Bookmarkit C# DotNetNuke DotNetPanel EF Code First Entity Framework HTC jQuery LINQ Migrations Networking Powershell Reddit SEO Telerik VB.NET Visual Studio 2010 Web Hosting Web Services WebsitePanel Websites Windows Azure Windows Server 2012 Windows Services WP7
  • Links

    • Stack Overflow
    • Twitter
    • Pluralsight Training
    • Me on Twitter
  • Stuff I work on

    Expeed Technology Sportsubs
Mystique theme by digitalnature | Powered by WordPress
RSS Feeds XHTML 1.1 Top
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.