Wednesday, August 29, 2018

Entity Framework (EF) Include() Lambda Extension Method Namespace

As of this writing, seems like Visual Studio still unable to provide suggestion on what namespace to import for extension methods.

I was looking into doing eager loading in EF and I am aware that I can use Include() method with lambda function. By default, it is not available and I can't remember which namespace it is located under. Of course, Visual Studio was not much help. After searching online, I found out that it is an extension method under System.Data.Entity namespace. I gotta remember from now on.


Monday, August 27, 2018

Entity Framework (EF) 6 Database.SqlQuery Mapping

I bumped into a problem with mapping results from manually crafted sql query to object using EF. Somehow, it doesn't seem to recognize the column attribute. And I found out that it really doesn't and they don't plan to enhance EF 6, although there seems to have the option in EF Core to do the mapping.

To illustrate my problem better, suppose my database column name is pk and would like to map it to ID property. Usually, using [Column("pk")] will solve the mapping, however, it doesn't work if the query was executed via dbContext.Database.SqlQuery("SELECT pk FROM TableName").

So I have to either change the ID property to pk or the one that I preferred is to change the query to dbContext.Database.SqlQuery("SELECT pk AS ID FROM TableName").

Thursday, August 23, 2018

How to Craft Multipart Form Data?

Ok, it is all started from me trying to send image from android to my web service using Google Volley. By default, Volley doesn't have built-in request to send image, but allows you to extend the request class. So, I need to extend the request class and create a MultipartFormDataRequest kind of class.

Reading after reading, I can't find the information I need to craft one. So I have to combine whatever I read with trial and error. The first one is the concept of boundary. It is a required information under Content-Type header. It acts as a separator between fields and can be any random string as long as it meets the requirements such as it can't exist in actual field data. More information such as length and size limit can be found under RFC 2046 Section 5.1.

So, the content-type header will be:

Content-Type: multipart/form-data; boundary=anyrandomstring

Next is how to use the boundary. The http body will start with two hyphens followed by the boundary. Underneath it is the Content-Disposition and Content-Type key value pair for each field. Between each field, it will be another two hyphens followed by the boundary as the separator. The one that caught me off guard was the closing boundary, it is two hyphens followed by the boundary and then followed by another two hyphens, so the http body will be:

--anyrandomstring

Content-Disposition: form-data; name=textFieldName
Content-Type: text/plain

TextFieldValue

--anyrandomstring

Content-Disposition: form-data; name=imageFieldName; filename=imageFilename.jpg
Content-Type: image/jpeg

[ImageBytes]

--anyrandomstring--


And then it works wonderfully. :)