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);
                        return "Succesfully Updated!";
                        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);
                        return "Succesfully Updated!";

                        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.

Saturday, October 6, 2012

[SQL/MySQL/.NET] Setting databases for .NET usage

After setting up WCF services and having my android devices consuming it, I wanted my WCF to be the layer that communicates to a database resource. At first, I had a hard time with getting started with SQL Server, so I went with MySQL instead.

NOTE: In your IIS, make sure the ASP.NET 4.0 Application Pool "Identity" is set to Local System. This is the reason why SQL Server wouldn't go so smooth at first, but I will discuss details later.

MySQL setup:

1. Download MySQL server installer, MySQL OBDC connector, and MySQL Net connector. Optionally, download the Workbench, which provides a GUI solution for handling SQL functions. If you're lame like me, you would use this tool.

2. Set up your tables using Workbench or commandline.

3. In Visual Studio, add a new ADO.NET Entity Data Model and point to your MySQL Server when prompted. If you don't know the server name or IP Addy, you can always go to the Workbench and see the properties there. Mine was Follow through with creating the .edmx file.

4. Now you should see the newly created entities right in front of you. The name of the object is exactly what the header of the entity is. So you can instantiate it like:

User objUser = new User();

Of course, we don't want to instantiate like this, but rather grab the info from the database and create the object then. We will use LINQ to do so.

 Now look for Entity Container Name in the properties of your .edmx file. This is the object you use to connect to the database. So lets just put 2 and 2 together with the entity object included:

MyDBEntities db = new MyDBEntities();
List<User> users = ( from p in db.Users select p ).ToList();

This should select all users from a "Users" table and return it as a list of User objects.

Run the WCF Test Client and test if it works.

SQL Server setup:

Okay, if you installed SQL Server 2008, I believe that it should've created an instance of SQLEXPRESS. So we will just go with this default setup, although, you might have set up another instance, you'll just have to remember the server name for that one.

So by default, your instance of SQLEXPRESS should be your <computername>\SQLEXPRESS. (ex. KETTLEPOT-PC\SQLEXPRESS).

Create tables in SQL Management Studio (Free) by using the server name.

From now on, follow step 3 from the MySQL setup section to configure everything in VS2010.

 Now, if you're getting the "underlying provider" error when you run the WCF Test Client and invoke your method, check out the "NOTE" at the beginning of this post.

Hope that helps!!!!

[Andoird/.NET] Android and WCF template

I've been working a lot with setting up WCF locally. Now it is time to do the true test and connect my android with the sucka!

To set up the WCF, just follow this post:

Setting Up WCF in Virtual Directories

All you need is Android and Eclipse set up. Also, get the KSoap plug-in and add it to your library. Now just add this to your code:

 private static String SERVICE_URI = "";

 private static final String METHOD_NAME = "GetData";

 private static final String NAMESPACE = "";

 private static final String URL = "";

 private static final String SOAP_ACTION = "";

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);


 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

 envelope.dotNet = true;
 HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);,envelope);
 SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
 String resultData = result.toString();

EDIT: Btw, the request.addProperty method is a parameter of your method. And if you don't know your namespace, look at the wsdl of your service. This was done to target an Android 2.2 device. If you noticed, I was using the template VS2010 gives when you create a new Web Service.

Also, I tried using HttpResponse and HttpEntity, but I got no response when I tried to do something like I believe it has something to do with the binding. By default, WCF uses WSHttpBinding I think. I tried changing to BasicHttpBinding, but the configuration had issues finding MetaData. That is it for now.

Friday, October 5, 2012

[.NET] Setting up WCF in Virtual Directories of IIS

Alright, this one was an adventure for me. So this info will be here for the next time I need to set up WCF in a virtual directory. This tutorial will start from the beginning, which is installing IIS. This is assumed that Visual Studio 2010 is already installed. So let's get started with the list:

1. Install IIS through Programs and Features. If you're feeling lazy, just try to install as much of the IIS options you can check. Otherwise, you just need ASP.NET under World Wide Web Services. This will auto-check dependencies as well. And IIS Metabase under IIS 6 Management Compatibilty. Also, you will need to go under the Microsoft .NET Framework 3.5.1 section, which is separated from the IIS section btw, and check both boxes under that. They should have the description with Windows Communication Foundation within it.

2. Make a new wcf service project or open an existing one. Go to it's project properties. Under the Web tab, select he use local IIS option. When you save, it will create the virtual directory for you. There are other ways to deal with the virtual directory like publishing, or adding it directory in the IIS manager. It is up to you.

If you get an error when you try to save the project properties, check if you have all the IIS components installed. VS2010 does a good job telling you what you need anyways.

Hmmm, that seemed to be a short list. But here are some problems + fixes that you may have to encounter.

Errors you may have encountered:

1. "Something about having a duplicate handler in your web.config". What happened here was that I tried to add a handler myself before installing the WCF stuff under Microsoft .NET Framework 3.5.1 section. Therefore, IIS couldn't even populate it's integrated handlers, let alone, let me add any more handlers. So I had to edit the web.config manually by copying it outside it's original directory, removing the handler, and then moving it back into the original directory. It will not let you edit it if you open it within the original directory.

2. 404.17: The requested content appears to be script and will not be served by the static file handler.

Check if you installed all the necessary components for IIS and WCF. You may have to run aspnet_regiis.exe -i under the command line if you have ASP.NET 4.0. After doing this, WCF should run smooth and you should be able to see the .svc file in your browser as intended.  This solution may be different if you are running an older version of ASP.NET.

3. Make sure  the application pools between the website and your virtual directory are ASP.NET 4.0. I haven't thoroughly tested this one out yet. I changed the website to ASP.NET anyways just to be safe. 

Local Host Stuff 

 If you tried to access your IIS or .svc from another computer, you may find that it will not connect by timing out or what not. This may be caused by the firewall. If you are using Windows Firewall, find and enable the inbound rule for World Wide Web Services. The port should be 80.


When on another computer, you may not want to type in just to get to connect to your service. You may want to type in http://MySite instead. This will be controlled in a DNS Server. The DNS settings should be located in your router firmware.

Thursday, October 4, 2012

[.NET] Unexpected error occurred in the .NET Framework...blah blah

  • An unexpected error occurred in the .NET framework data provider for Microsoft SQL Server Compact 3.5. Please contact the provider vendor to resolve this problem.

    If you're getting the error above, while trying to view your connections or trying to generate a new entity data model, try this:

    Go to your registry.
    Navigate to:
    And delete anything that has .NET framework data provider for Microsoft SQL Server Compact