Avoid Adding Duplicate Records in ASP.NET using Entity Framework 5
Posted by Aang in Entity Framework on February 11, 2013
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.
Installing Project Pier on Ubuntu Server
Posted by Aang in Project Management, Project Pier on December 30, 2011
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)
Prerequisites:
- 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”.
- GIT
-
sudo apt-get install git-core
-
The Steps:
-
sudo apt-get install php5-gd
-
sudo git clone git://github.com/phpfreak/Project-Pier.git /var/www/projectpier
- 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
-
- 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
-
- 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
-
- Create a Virtual Host on your Apache Server (see screenshots)
- Restart Apache
-
sudo /etc/init.d/apache2 restart
-
- Open up FireFox, and navigate to:
-
http://localhost/projectpier/public/install/
-
- Follow the instructions
- If all goes well you’ll see:
- 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
-
- In FireFox, navigate to:
-
http://localhost/projectpier/public/
-
- You will now be asked to set up an Admin user
- Get going, and enjoy Project Pier
Hope this was helpful.
Comments, questions, and corrections are always welcome
Just leave them below.
End Of Line.
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
Prerequisites:
-
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”.
-
Ruby Version Manager (RVM)
- To install, follow the instructions here: https://rvm.beginrescueend.com/rvm/install/
Steps for the Install:
For the following steps, I adapted the instructions found here: https://github.com/ari/jobsworth.
-
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
-
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
-
Install Bundler Gem – for more info on Bundler go here: http://gembundler.com/
-
rvm 1.9.2 do gem install bundler
-
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.
-
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
-
Get the source
-
sudo git clone git://github.com/ari/jobsworth.git /var/www/jobsworth
-
Change the permissions for the jobsworth directory
-
sudo chown -Rv $USER /var/www/jobsworth
-
Setup a Virtual Host on Apache using Webmin
-
Log into your local instance of Webmin – default = https://localhost:10000 (sceenshot)
-
Create a Virtual Host for the jobsworth application (screenshot)
- Servers –> Apache Webserver –> Create Virtual Host
- see screenshot for the proper settings
- Click Create

-
Select the Virtual Host you just created (screenshot)
-
Click on Edit Directives (screenshot)
-
Change your Directive file, ***ignore the text in the sceenshot, copy the text BELOW the screenshot***

-
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
-
Now restart your Apache Server
-
sudo /etc/init.d/apache2 restart
-
Install the Gems that JobsWorth needs to run
-
cd /var/www/jobsworth
-
rvm 1.9.2 do bundle install
-
Setup the database
-
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
-
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
-
Now create the database:
-
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
-
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:
- Open the setup.rb file, located in /var/www/jobsworth/setup.rb
- 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
-
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”.
-
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
-
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
-
Start using JobsWorth
- Try accessing JobsWorth on your server first – in FireFox –> http://wiki.yourmadeupdomain.com
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.
HTC EVO 4G not Sending/Receiving MMS Messages :(
Posted by Aang in Android, HTC EVO 4G, Phone on September 13, 2011
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
- Download “APN Backup & Restore” from the Market
- Use the App, and first run Backup APNs
- Name the file: APN-Broken.xml
- 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> - Save your changes as APN-Fixed.xml
- Disconnect your phone from your PC, and run APN Backup & Restore again
- Now run Restore APNs
- Choose APN-Fixed.xml to apply it
- Perform a Reboot on your phone, and you’re all set.
Leave you comments or questions below
End of Line.
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)
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:
- curl
- 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…..
- Go to http://nodejs.org/#download
- Copy link location
- Open up Terminal
- Enter your Downloads directory
-
cd Downloads/
- 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
- Once NodeJS is downloaded, unzip the file
-
tar -xvf node-v0.4.7.tar.gz
- Change Directory to Node Directory you just unzipped
-
cd node-v0.4.7/
- Configure and Make, the making could take a while, so go grab yourself a coffee after you run the make command
- Verify NodeJS got installed
- You now have Node.JS installed, you can delete the stuff you downloaded and unzipped
3. Install Node Package Manger
- 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
- Install Node Package Manager
-
curl http://npmjs.org/install.sh | sh
- Verify Node Package Manager (npm) got installed
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.
Free Remote GIT Repository with Dropbox
Posted by Aang in Dropbox, GIT, Source Control on April 13, 2011
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:
- 2 tablespoons of Windows (XP or 7) – however I am sure this can be applied to Linux or Mac
- 1 cup of Dropbox (can download it here)
- 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
- Create two folders:
- Remote_Repository (Create this inside your Dropbox folder)
- Local_Project (I just created this on my Desktop)
- 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
- Let’s go ahead and create our initial commit, so our repository is “setup”
- Now we need to git clone –bare our Local_Project repository (Note: I am changing directory. In screenshot below, I am one directory higher)

- For more information on git clone, see here
- 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)
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.


















