Avoid Adding Duplicate Records in ASP.NET using Entity Framework 5

So I have been playing around with ASP.NET Entity Framework 5, and I am a big fan of Code First development, especially the reverse engineering code first plugin, which you can learn about here.

However, this is not what this article will be about. I ran into an issue in which I had multiple types of users, however a single user could have multiple types (i.e. – Approver and Requestor). I was adding the users one type at a time, and in one scenario I was getting duplicate records being inserted. An example, Bob is a user who is not yet in the database. For a given request, Bob is designated as the Requestor, so I add him to the DB context. Later in code I determine that Bob is the Approver as well, so he again gets added to the DB Context. Upon reaching SaveChanges(), Bob gets inserted twice into the database. That’s not what I wanted.

The code below will remedy that scenario. I check the database for user Bob, but I will also check the current context to avoid adding Bob twice:

private Repository.Models.User GetUser(Repository.Models.DBContext db,
            string emailAddress)
        {
            var user = db.Users.FirstOrDefault(x => x.EmailAddress == emailAddress);

            if (user == null)
            {
                user = db.Users.Local.FirstOrDefault(x => x.EmailAddress == emailAddress);
            }

            return user;
        }

Questions or comments below.  Hope this adds some value for some :-)

End of Line.

, , ,

Leave a comment

Installing Project Pier on Ubuntu Server

As of late, I have undertaken the quest in search of a small-business, open-source, project management/wiki/file storage solution.  Oh, and I hope the “open-source” adjective conveyed that it needs to be a free (as in beer) solution as well.

I initially thought JobsWorth was the solution I was looking for, but after many many many many many many many…many hours of getting the server setup, only to find it too buggy for my tastes, I gave up.  The installation for JobsWorth works however, and if you want to give it a try, I have the steps listed in this blog entry.

I have looked at many other options during my quest, such as Redmine, Trac, SugarCRM, TWiki, etc.  Without going into all that detail, the winner was Project Pier.  It fits all the criteria above, and while I immediately found a bug, I submitted an issue ticket, and BAM, WAP, ZOOM the developer is working to get it fixed, so I am very optimistic.  And since it is on GIThub, applying the fix, will be cake :)

All that said, here is a tutorial to get Project Pier, version 0.8.8, setup on Ubuntu 10.04 Server Edition.

These of course are taken from the Project Pier Installation page (http://www.projectpier.org/manual/installation/).

My Setup:

  • Ubuntu 10.04 Server Edition (32-bit)
    • JobsWorth Install - Server Settings
    • During Server Setup chose (as seen in screenshot):
      • LAMP Server
      • OpenSSH Server
      • SAMBA Server

Prerequisites:

  1. Webmin
    • This is for easy setup of apache – to install, follow the instructions here: http://www.webmin.com/deb.html.  Go to section “Using the Webmin APT repository”.
  2. GIT
    • sudo apt-get install git-core

The Steps:

  1. sudo apt-get install php5-gd
  2. sudo git clone git://github.com/phpfreak/Project-Pier.git /var/www/projectpier
  3. Locate the following directories and change the permissions to 777 (read/write/execute for all users):
    • sudo chmod -v 777 /var/www/projectpier/public/files;sudo chmod -v 777 /var/www/projectpier/cache;sudo chmod -v 777 /var/www/projectpier/upload;sudo chmod -v 777 /var/www/projectpier/config;sudo chmod -v 777 /var/www/projectpier/tmp
  4. Locate the following file(s) and change the permissions to 777 (read/write/execute for all users):
    • sudo chmod -v 777 /var/www/projectpier/cache/autoloader.php;sudo chmod -v 777 /var/www/projectpier/cache/log.php;sudo chmod -v 777 /var/www/projectpier/upload/attributes.php
  5. You MUST create tho database first
    • echo "CREATE DATABASE projectpier DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; GRANT ALL ON projectpier.* TO 'projectpierDBA'@'localhost' IDENTIFIED BY 'keepitsecretkeepitsafe'; FLUSH PRIVILEGES;" | mysql -u root -p
  6. Create a Virtual Host on your Apache Server (see screenshots)
    • Create Virtual Host with Webmin
    • Create Project Pier Virtual Host
  7. Restart Apache
    • sudo /etc/init.d/apache2 restart
  8. Open up FireFox, and navigate to:
    • http://localhost/projectpier/public/install/
  9. Follow the instructions
    • When you get to “Step 3: Database configuration” (see screenshot)
      • Project Pier Installation - Step 3
    • If all goes well you’ll see:
      • Project Pier Installation - Success
  10. Locate the following file(s) and change the permissions to 777 (read/write/execute for all users):
    • sudo chmod -v 777 /var/www/projectpier/config/config.php
  11. In FireFox, navigate to:
    • http://localhost/projectpier/public/
  12. You will now be asked to set up an Admin user
  13. Get going, and enjoy Project Pier :)

Hope this was helpful.

Comments, questions, and corrections are always welcome :)  Just leave them below.

End Of Line.

2 Comments

Setting up a JobsWorth Server in Ubuntu

JobsWorth is a branch of the very awesome Rails application called Clocking IT.  So in order to do my part to help out, I would like to provide a how-to for setting up JobsWorth with Ubuntu Server Edition 10.04.

More information about Jobsworth found here: https://github.com/ari/jobsworth

and here: http://www.clockingit.com/

and here: http://groups.google.com/group/jobsworth

Ok, let’s get started!

My Setup:

  • Ubuntu Server Edition 10.04.3 – 32 bit
    • During Server Setup chose (as seen in screenshot):
      • LAMP Server
      • OpenSSH Server
      • SAMBA Server
      • JobsWorth Install - Server Settings

Prerequisites:

  1. Webmin

    • This is for easy setup of apache – to install, follow the instructions here: http://www.webmin.com/deb.html.  Go to section “Using the Webmin APT repository”.
  2. Ruby Version Manager (RVM)

Steps for the Install:

For the following steps, I adapted the instructions found here: https://github.com/ari/jobsworth.

  1. Install the follow packages, that will be needed to run JobsWorth

    • apt-get install mysql-server apache2 git-core imagemagick gcc libc6-dev g++ zip build-essential bison openssl libreadline6 libreadline6-dev curl zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev autoconf libcurl4-openssl-dev apache2-prefork-dev libcurl4-openssl-dev libaprutil1-dev libapr1-dev libxslt-dev libxml2-dev openjdk-6-jre
  2. Install Ruby version 1.9.2 ***Note: You need to have followed the instructions here, and have Ruby Version Manager setup and working

    • rvm install 1.9.2
    • rvm --default use 1.9.2
  3. Install Bundler Gem – for more info on Bundler go here: http://gembundler.com/

    • rvm 1.9.2 do gem install bundler
  4. Install Phusion Passenger

    • rvm 1.9.2 do gem install passenger
    • rvm 1.9.2 do passenger-install-apache2-module
      • This will setup Apache for you
    • Once passenger sets up Apache for you, at the end of a successful process you’ll see a message like:
      • The Apache 2 module was successfully installed.
        Please edit your Apache configuration file, and add these lines:
        LoadModule passenger_module /home/tron/.rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.11/ext/apache2/mod_passenger.so
        PassengerRoot /home/tron/.rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.11
        PassengerRuby /home/tron/.rvm/wrappers/ruby-1.9.2-p290/ruby
        After you restart Apache, you are ready to deploy any number of Ruby on Rails
        applications on Apache, without any further Ruby on Rails-specific
        configuration!
        Press ENTER to continue.
        • Note:  Use your Username in the paths above. (just in case you didn’t notice :))
    • Don’t worry about the lines for the Apache configuration file – we’ll deal with those in a later step.
  5. Modify your “hosts” file

    • If your like me, and you are installing your server on an Intranet, you’ll need to modify your “hosts” file so Apache knows where to go.  Additionally, the JobsWorth setup needs a domain.
    • gksudo gedit /etc/hosts
    • Add this line to your “hosts” file – remember the domain you use, you’ll need it later
      • 127.0.0.1    wiki.yourmadeupdomain.com
  6. Get the source

    • sudo git clone git://github.com/ari/jobsworth.git /var/www/jobsworth
  7. Change the permissions for the jobsworth directory

    • sudo chown -Rv $USER /var/www/jobsworth
  8. Setup a Virtual Host on Apache using Webmin

    1. Log into your local instance of Webmin – default = https://localhost:10000 (sceenshot)
      • JobsWorth Install - Log into Webmin
    2. Create a Virtual Host for the jobsworth application (screenshot)
      • Servers –> Apache Webserver –> Create Virtual Host
        • see screenshot for the proper settings
      • Click Create
      • JobsWorth Install - Webmin - Apache Virtual Host
    3. Select the Virtual Host you just created (screenshot)
      • JobsWorth Install - Webmin - Select Virtual Host
    4. Click on Edit Directives (screenshot)
      • JobsWorth Install - Webmin - Edit Directives
    5. Change your Directive file, ***ignore the text in the sceenshot, copy the text BELOW the screenshot***
      • JobsWorth Install - Webmin - Virtual Host Directive
      • DocumentRoot "/var/www/jobsworth/public"
        ServerName wiki.yourmadeupdomain.com
        RailsEnv production
        PassengerHighPerformance on
        LoadModule passenger_module "/home/tron/.rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.11/ext/apache2/mod_passenger.so"
        PassengerRoot "/home/tron/.rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.11"
        PassengerRuby "/home/tron/.rvm/wrappers/ruby-1.9.2-p290/ruby"
        <Directory "/var/www/jobsworth/public">
        AllowOverride All
        Options -MultiViews +Indexes
        Order allow,deny
        Allow from all
        </Directory>
        • Note:  Use your Username in the paths above. (just in case you didn’t notice :))
      • Click Save
    6. Now restart your Apache Server
      • sudo /etc/init.d/apache2 restart
  9. Install the Gems that JobsWorth needs to run

    • cd /var/www/jobsworth
    • rvm 1.9.2 do bundle install
  10. Setup the database

    1. As of the time of this writing there were some database setup issues.  The following steps are a work-around.  If you have the more “up-to-date” steps, please comment and I will update this tutorial :)
    2. You have to set up your database.yml file manually.
      • Make sure you are in the right directory:
        • cd /var/www/jobsworth
      • cp config/database.example.yml config/database.yml
      • Mine looks like this:
        • development:
            adapter: mysql2
            database: jobsworth
            username: jobsworthDBA
            password: keepitsecretkeepitsafe
            host: localhost
            encoding: utf8
          
          test: &TEST
            adapter: mysql2
            database: jobsworth_test
            username: jobsworthDBA
            password: keepitsecretkeepitsafe
            host: localhost
            encoding: utf8
          
          production:
            adapter: mysql2
            database: jobsworth
            username: jobsworthDBA
            password: keepitsecretkeepitsafe
            host: localhost
            encoding: utf8
          
          cucumber:
            <<: *TEST
          selenium:
            <<: *TEST
          
          staging:
            adapter: mysql2
            database: jobsworth_staging
            username: jobsworthDBA
            password: keepitsecretkeepitsafe
            host: localhost
            encoding: utf8
    3. Now create the database:
      1. echo "CREATE DATABASE jobsworth DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; GRANT ALL ON jobsworth.* TO 'jobsworthDBA'@'localhost' IDENTIFIED BY 'keepitsecretkeepitsafe'; FLUSH PRIVILEGES;" | mysql -u root -p
  11. Modify Ruby Setup File

    • As of the time of this writing (12/20/2011) the Dev made a change that broke the setup (at least for Ubuntu), so you’ll need to change it back.  Look for the following code, and make sure it looks like what I have below:
      1. Open the setup.rb file, located in /var/www/jobsworth/setup.rb
      2. Go to line 123, and make sure the code looks like this:
        • puts "Loading Rails to create account..."
          begin
            require File.expand_path('../config/environment.rb', __FILE__)
          rescue
            puts "*** Unable to load Rails, please ensure you have a working Rails environment. ***"
            exit
          end
        • Save changes
  12. Run the Ruby Setup File

    • cd /var/www/jobsworth
    • ruby setup.rb
    • Answer the first question “Enter hostname for the Jobsworth service (for example projects.mycompany.com):”
      • wiki.yourmadeupdomain.com
    • When you get to the question “Initialize database schema [n]:”, answer “y”.
  13. Email Setup

    • I have not yet gone through the email setup since my need is only for a local server.  The directions are located at “Step 8: Sending email” following this link: https://github.com/ari/jobsworth
  14. Finishing up

    • sh update.sh
    • ***Note: Could someone please comment and let me know if I am able to setup the Email after I run the “sh update.sh”????  I’m not going to need email for a while, so I have not set it up this tutorial, but I will and I don’t want to have to start all over to just setup Email.  PLEASE PLEASE let me know if you know
  15. Start using JobsWorth :)

That will do it.  Hope this helps some folks out! :)

And again, a big thanks to the JobsWorth Dev Team – thanks for this great tool! :)

End of Line.

15 Comments

HTC EVO 4G not Sending/Receiving MMS Messages :(

Problem:

HTC Sense default messaging unable to send or receive MMS messages :(  Every time I try to send an MMS, I eventually receive the error message: “generic network failure”.

My Setup:

Phone:  Sprint HTC EVO 4G

OS:  Fresh Evo 4.2 (4.2.0.0) by flipz

Solution:

As usual, I cannot take credit for my ingenious solutions ;)  You can find the source of my plagiarized solution here.

Note:  You DO NOT need the be rooted for this solution.  However, if you’re not rooted, you should be ;P

  1. Download “APN Backup & Restore” from the Market
  2. Use the App, and first run Backup APNs
    • APN BackRestore
  3. Name the file: APN-Broken.xml
  4. Connect your phone to your computer as “disk drive”, using notepad open APN-Broken.xml, and correct the code to match:
    • <?xml version=’1.0′ encoding=’UTF-8′ standalone=’yes’ ?>
      <apns count=”1″>
      <apn name=”Production” numeric=”00000″ mcc=”000″ mnc=”00″ apn=”1” user=”null” server=”null” password=”null” proxy=”null” port=”null” mmsproxy=”null” mmsport=”null” mmsprotocol=”2.0″ mmsc=”http://mms.sprintpcs.com” type=”mms” />
      </apns>
  5. Save your changes as APN-Fixed.xml
  6. Disconnect your phone from your PC, and run APN Backup & Restore again
  7. Now run Restore APNs
    • APN BackRestore02
  8. Choose APN-Fixed.xml to apply it
  9. Perform a Reboot on your phone, and you’re all set.

Leave you comments or questions below :)

End of Line.

, , , ,

16 Comments

Gnome Icons in the Unity Bar

Problem:

I installed Cryptkeeper on my Ubuntu 11.04, but when I run it, the only way to manage it is through the icon it places in the top desktop bar, or Panel.  You know that top desktop bar that used to hold my wonderful applet icons, yeah this one:

Unity Desktop

Setup:

Ubuntu 11.04 64 bit (Natty Narwhal)

Asus G73JW-A1 (RoG)

Solution:

So if you want to see your icons in the top bar again, run this command from the Terminal, and once you’re done log out and back in:

gsettings set com.canonical.Unity.Panel systray-whitelist "['all']"

End of Line.

, , ,

Leave a comment

Install Node.JS on your Ubuntu Machine

So I recently heard about this thing called Node.JS.  To be honest, I’m a little skeptical, but I like what I have seen so far on the Node.JS website, and on some webcasts.

I like hands-on learning, so while I was going through the how-to videos I wanted to use Node.JS to follow along.  So, here are the steps to get Node.JS and Node Package Manager on your Ubuntu machine.

Here is my setup:

Ubuntu 11.04 64 bit (Natty Narwhal)

Asus G73JW-A1 (RoG)

I am installing on a VMWare Natty Narwhal 32 bit image.

1. Install some needed packages

You’ll need some packages from synaptic to start:

  1. curl
  2. libcurl4-openssl-dev

So open up Synaptic Package Manager, and install those 2 packages (and of course the dependencies too).  If you don’t know how to install the packages, you probably should not be trying to use NodeJS, stick with ASP.NET MVC ;)

2.  Install and Build NodeJS

So according to the webcasts I have seen, since NodeJS in “bleeding edge”, it is best to download and compile from the source.

Soooooo…..

  1. Go to http://nodejs.org/#download
  2. Copy link location
    • Nodejs Download Link Location
  3. Open up Terminal
  4. Enter your Downloads directory
    • cd Downloads/
  5. Enter the command in Terminal: curl -O, then paste the link you copied (should look like below)
    • Note: The version number in the link will most likely change as of the time of this writing
    • curl -O http://nodejs.org/dist/node-v0.4.7.tar.gz
  6. Once NodeJS is downloaded, unzip the file
    • tar -xvf node-v0.4.7.tar.gz
  7. Change Directory to Node Directory you just unzipped
    • cd node-v0.4.7/
  8. Configure and Make, the making could take a while, so go grab yourself a coffee after you run the make command
    • ./configure
      • if configure succeeds it should say: ‘configure’ finished successfully
      • Configure Succeeded
    • sudo make && sudo make install
    • Node Build Succeeded
  9. Verify NodeJS got installed
    • node --version
    • Node Version
  10. You now have Node.JS installed, you can delete the stuff you downloaded and unzipped

3.  Install Node Package Manger

  1. For starters we do NOT want to do Node Package Management with sudo, so according to this article, this is our first command
    • Open Terminal
    • sudo chown -R $USER /usr/local
  2. Install Node Package Manager
    • curl http://npmjs.org/install.sh | sh
  3. Verify Node Package Manager (npm) got installed
    • npm --version
    • Node Package Manager Install

Congratulations!  You now have NodeJS and Node Package Manger installed on you Ubuntu machine!  Time to start coding with NodeJS!  I don’t know how to yet, but you need to start somewhere :)

Please add comments or questions below.

End of Line.

1 Comment

Free Remote GIT Repository with Dropbox

Introduction

Let me first start off by saying I love GIThub.  I would recommend that for any small or large business that would like a source control hosting solution.  However, this tutorial is for those who have maybe a personal side project, and they want their project kept nice and safe on the cloud and under source control :)

So here are the ingredients:

  1. 2 tablespoons of Windows (XP or 7) – however I am sure this can be applied to Linux or Mac
  2. 1 cup of Dropbox (can download it here)
  3. 1 cup of GIT (see note below)

Note: So you can download GIT here.  However, I used GITextensions, which installs GIT and integrates it very nicely with Visual Studio 2010 (or 2005/2008).  You can download GITextensions here.

Assumptions

  • I am assuming you have all of the ingredients above up and running.
  • I am assuming you are familiar with Dropbox, this is not a tutorial on setting Dropbox up.  (It’s really quite easy :) )
  • I am assuming you have minimal knowledge of GIT (my definition of “minimal knowledge” of GIT is that you have at least watched this video)

The Good Stuff

So now lets git started :)

  1. Create two folders:
    • Remote_Repository (Create this inside your Dropbox folder)
    • Local_Project (I just created this on my Desktop)
  2. The Local_Project folder will have the development project your working on – Navigate to the Local_Project folder (using Git Bash), and create your GIT repository using the git init command
    • Initialize GIT Repository
  3. Let’s go ahead and create our initial commit, so our repository is “setup”
    • GIT Initial Commit
  4. Now we need to git clone –bare our Local_Project repository (Note: I am changing directory.  In screenshot below, I am one directory higher)
  5. Now we just need to setup our “local” repository to track our “remote” repository.  That way when we do things like git push or git fetch, GIT will know where to look. (Note: I am back in the Local_Project directory)
    • Add remote repo to GIT

Now you should be able to branch, commit, fetch, etc in your Local_Project, and when you git push to will go to the Remote_Repository folder, which is in your Dropbox folder, so it is automatically synced remotely :)

I have to admit, I have not tried this if you have multiple developers.  But again, I wanted smaller, personal side projects to have a remote source control solution.

Hope someone finds this useful.  Please feel free to add comments or questions below.

End of Line.

, ,

Leave a comment

Follow

Get every new post delivered to your Inbox.