Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Monday, November 19, 2012

[PHP/MySQL] Amazon EC2, Elastic Beanstalk, RDS setup

Basic setup of using these three services in.....haaaarrrmony. I'm using WinXP.

Index:
1. Setup EC2 instance
2. Setup Elastic Beanstalk with GIT (Tested with PHP projects)
3. Setup RDS to be used with MySQL Workbench

1. EC2 Instance
   Just follow the instructions and CREATE and DOWNLOAD your key/pair. Creating your instance is the only time you can assign a key/pair to it, let alone download it. Take note of the security group you are using for it. Also, go find your internet IP and add it to the selected security group.

2. Elastic Beanstalk
   This is basically your web host that you deploy to. Download GIT and the CLI Package (http://aws.amazon.com/code/6752709412171743). Find GIT on Google and the CLI Package in the Elastic Beanstalk documentation. Install GIT and run the CLI Package according to the instructions in the archive. 

Configuring Beanstalk for file uploading/deploying:
Go to your working folder in command line.
First, type eb init
Follow the instructions and you can find the your credentials here: https://aws-portal.amazon.com/gp/aws/securityCredentials
After you're done, enter the command: eb start

Now, you should be able to run git commands. Usually, this is the sequence I use:
1. git add .    (adds all files/folders/sub-folders)
2. git commit -m "<your message>"
3. git aws.push

3. RDS
1. Choose/create a database instance you would like to work with.
2. Go to the DB Security Groups section.
3. Add a new connection type of EC2 Security Group and choose the same one used in your EC2 Instance. You may also want to add a new CIDR/IP address using an IP finder.
-- MySQL Workbench
4. Setup a New Server Instance
5. Select Remote Host and input the address of your EC2 instance.
6. Select SSH type of connection
7. For SSH user, put ec2-user if using Amazon or ubuntu if using Linux
8. SSH password is not required.
9. Use the key/pair generated earlier
10. Enter the rest with the DB Instance info.

After all that, you should be good to go. Cheers



Monday, October 29, 2012

[.NET] Updating Detached Objects in Entity Framework

Here's the scenario.

An object is being passed like this


  1. Object gets pull from the database through the Business Logic layer and WCF, as well as getting converted into a detached object.
  2. Values are changed in the Business Logic Layer.
  3. Object gets passed back into the WCF for an update.
I've seen several ways of how a detached object could be updated. Previously in the data access layer, I was retrieving the original object from the database and getting the object again by the original's entity key. Then updated the values and applied the changes. It looks something like this:

using (MyDBContext db = new MyDBContext ())
                {

                    My_Object obj =  db.GetObjectByKey(GetObjectFromDatabaseByID(id).EntityKey) as My_Object;

                    obj.Name = <newName>;

                    if (obj != null)
                    {
                        db.ApplyCurrentValues("My_Object_Table_Name", obj);
                        db.SaveChanges();
                        return "Succesfully Updated!";
                    }
                    else
                        return "Record Does Not Exist.";
                }

So one thing, the method that surrounds this code is only passing an 'id'. There is no objects coming in. Therefore, the object would have to be retrieved again to use its Entity Key to grab the object again, except with an attached state. Another parameter that was passed into the method was the newName. So this newName variable has to be assigned to the new attached object. Only then, we can apply and save the changes. Yes, this is a crude way of doing it, but it works.

Recently, I found a new way of handling an update. So what if we pass in a whole object? Will we have to consider the change of all members of the object? What if the object has 50 members? Well, coding will definitely suck. This method was posted on MSDN, so I'm just going to take it as a valid strategy, and it goes something like this:

 EntityKey key = default(EntityKey);
                object originalObj = null;

                using (MESDataModel.e_MESEntities db = new MESDataModel.e_MESEntities())
                {
                    key = db.CreateEntityKey("KIT_Part", ct);

                    if (db.TryGetObjectByKey(key, out originalObj))
                    {
                        db.ApplyCurrentValues(key.EntitySetName, ct);
                        db.SaveChanges();
                        return "Succesfully Updated!";
                    }
                    else
                    {

                        return "Record Does Not Exist.";
                    }

                }

In this situation, we are creating an entity key out of the passed in object. If it already has a key, then it will return the original instead of creating a new one. Similar to the first method, we are using the key to retrieve an attached object. At this point, the attached object and the passed in object now has the same keys. Call ApplyCurrentValues and the data from the passed in object will write to the attached object within context. This is pretty much MSDN's definition of what ApplyCurrentValues does. Save changes and everything should work. Hope this kind of helps.

Hmm, Okay, what about using DetectChanges?
This method seems to be good, if you are pulling straight from the database, writing new values, and saving it back to the database. No detached objects involved here. If there is a way to use this method with cross platform in mind (Android, iPhone), please let me know.