Categories
Uncategorized

Using Entity Framework: The CodeFirst Approach

When I started into the world of .NET based web development, I often encountered the term “Entity Framework Code First” or “EF Code First”. Where as I could no where find the option to create “Code First” entities in the visual studio. 🙁 How disappointing! 😛 I could create ADO.NET Entity Data Model but that would create .edmx files which looked all cryptic to me! I was just beginning .NET and my background is heavily based on PHP and Python where nothing is “Visual”, we do it all in codes.

And then after going through some of the resources online, I finally understood what’s going on. The ADO.NET Entity framework has two popular approaches – “Database first” and “Code first”. As the names suggest, in the first approach you write your models, generate databases, map the database with your models etc. Here database comes first. On the other hand, in the “code first” approach, you write plain classes which are identical to your database objects. Then following specific convention, you magically map the defined classes with the database. No need to write (I mean generate) complex xml maps or all those mess.

While I agree that both the approach have their own benefits, strengths and all those positive things, I found the code first approach easier to get started as a novice. So how does it work? Let’s see!

Say, we already have a table defined somewhere. For convenience, let’s use the following SQL script to generate a table:

I am not going to elaborate the process but the direction is to first create a database and then use the above sql codes to generate a table. It has one ID field that is auto incremented and the Name field. The simplest table structure I could think of! 😀

Now, in code first approach, convention is prioritized over configuration. Hence, we must define an identical class. Here it goes:

Remember those “virtual” identifiers are crucial. They have a lot to do with the magic that keeps things going. Another interesting thing to notice is that the Student object doesn’t derive from any other object.

Now, we need a database context. We shall define a class that will extend the “DbContext” class from the Entity Framework. The code should be like this:

One thing to notice is that we must pass a database connection string to the DbContext class. In this case, I have a connection string defined in my configuration file and I am passing the name of the connection to the DbContext class.

With this, we’re ready to use the Entities we have generated. So, how do we use them? Let’s see some codes:

The above codes will insert a entry in the database when run. Give it a try! 🙂

PS: I am just beginning .NET and C#. If you have any comments, suggestions or plain feedback, please do feel free to leave in the comments. I would appreciate your contribution to my personal development 🙂

Categories
Uncategorized

Fixing Error 29506 while Installing SQL Server Management Studio

I was trying to get SQL Server Management Studio installed when I encountered the error 29506. Looking up on the internet revealed that it’s related to admin privilege. When I view the properties of the installer, under the compatibility tab, “Run as administrator” option is disabled.

So instead, from start menu, I ran command prompt in administrator mode. Changed directory to the folder where the installer was. Ran the installer from command line and it all worked 🙂

Categories
Uncategorized

Fixing the Google+ Issue on Bangla Lion

For some reasons, many Bangla Lion users are randomly not being able to consume many static contents from Google+. They are also not being able to download any email attachments from Gmail. What’s happening? Google serves their content from googleusercontent.com. Gmail attachments are also served from a sub-domain of this domain. For some unknown reasons we can not access that domain sometimes, sometimes we can.

Just a few moments ago, when I complained to Bangla Lion customer care, the customer manager helped me fix the problem. What we did?

1) First we set no proxies. On my Firefox 5, I had proxies set to system proxies. Bangla Lion’s customer manager suggested that I should set no proxy or automatic detection for proxies. I changed it to no proxy. For this, I went to Firefox Menu > Options > Options > Advanced > Network > Settings (I am using FF5).

2) Then I went to “Network and Sharing Center” > “Change Adapter Settings”. I right clicked on my Bangla Lion connection (I am using the indoor device, so mine is a wired network) and selected “Properties”. In the “Networking” tab, I double clicked on the TCP/IPv4 entry. The dialog box I got, it let me define how my IP address and DNS addresses are obtained. I manually set the DNS Servers to: 4.2.2.8 & 4.2.2.2 .

The customer manager also gave me another pair of DNS in case I still have an issue: 4.2.2.5 & 8.8.8.8
This was supposedly a good sign of proper customer service, which I had read the other day on https://www.salesforce.com/products/guide/lead-gen/.

3) I cleared my browser’s cache and everything was okay. To test things, I have restarted my router several times to see what happens when I am assigned to different IP address. Everything looks fine.

So, the description I gave so far is very specific to Windows 7 and Firefox. A general instruction for any OS and any browser should be:

1) Make sure you’re not using any proxies.

2) Change your network’s DNS to: 4.2.2.8 & 4.2.2.2 or 4.2.2.5 & 8.8.8.8

3) Clear the cache and offline data.

I am satisfied with Bangla Lion’s customer care, at least they put some people who knows something. When GP started image optimization, it was a night mare. +1 to Bangla Lion for their informative customer care service.