Entity Framework 6 – What’s new ?

EF6 is still in alpha stages but you can go grab it now via nuget.
What is new ?
A whole lot find about it here EF6

Also, do read this post about migrating to EF6 if you are migrating an existing application.
EF6 Migration

I was able to use a few of the features that are new. I had earlier blogged about EF(EF 4.1) when V4.1 came out, I am basically using the same model to see if life is any easier with V6. The model is fairly simple. There are users, users can questions and questions are of different types. A user creates Test which is composed of many questions.

Earlier, there was no easy way of doing Enum in EF Code First. That limitation is no longer there.
Now, the questions can be of varying difficulty levels so now we can do the following.

    public enum Difficulty
        Trivial = 1,
        Easy = 2,
        Medium = 3,
        Hard = 4,
        Difficult = 5

    /// <summary>
    /// Question is the base type which describes a Question. Other types derive from Question.
    /// </summary>
    public class Question
        /// <summary>
        /// Gets or sets the Difficulty
        /// </summary>
        public Difficulty Difficulty { get; set; }

....more properties 

Similarly we can have UserRole as well.

EF now has the option to specify the default schema is also available now. It can be done as shown below.


EF now has the ability to provide custom conventions as well. The following conventions are available to us.

  • Lightweight Conventions
  • Configuration Conventions
  • Custom Attributes
  • Model-based Conventions

Read more about it here EF Conventions.
I will cross-post an important point about the order of execution. If you have many conventions then the order will become important. I haven’t written a lot of conventions but my guess is that this could get tricky if not done with care.

  1. IConfigurationConvention-based and lightweight conventions
  2. IEdmConvention-based conventions
  3. IDbConvention-based conventions
  4. IDbMappingConvention-based conventions

A conventions that I have used is Lightweight conventions. We sometimes want to append custom names to our table names as per the convention of our customers.

modelBuilder.Entities().Configure(_ => _.ToTable("CustomTable"+ _.ClrType.Name));

Similarly, other conventions are also available. The most common ones that I see being used will be Lightweight and Configuration Conventions.

Next, we will have a look at enabling migrations and an irritant that made me go nuts for a few months.

Console 2 – Command Line awesomeness

1. What is this ?
Ever felt that the command prompt fall a little short of your expectations ? You want it to feel like the power toll and not just a tool.

2. Where do I get it ?
You can download it from here Console2

3. Help me set it up .
Refer to Scott Hansleman’s blog post Original Post.

Now, if you have successfully managed to get Console2 working note carefully what the last part in Scott’s post said
Console2 is a great little front-end for your existing shell, no matter what it is. Note that Console2 isn’t a shell itself, it’s just a face on whatever you are already using.

4. Let’s make the world a better place by adding Git Bash to the the tabs.

Title : GitBash
Icon : C:\Program Files (x86)\Git\etc\git.ico
Shell : C:\Program Files (x86)\Git\bin\sh.exe --login -i
Startup dir : c:\Users\Ashutosh ( or whatever your path is )



All looks nice, the world is a better place.

Entity Framework – Generic Repository Pattern – Part 2

In the previous post I wrote about an implementation of the Generic Repository pattern for EF. There is so much data on the web that it is difficult to understand what might actually work for you. All of this can drive you Crazy. Throw in my version of crazy as well Crazy2

Now that you have checked out the links and gone crazy reading how we all wanted a code review for this, it tells you the developer about the levels of confusion this pattern can create. One of my first thoughts was something like this Honestly, I don’t even remember what my exact thoughts were when I was prototyping the code.

In Part1 I did say that I will highlight some of the issues that I faced (remember YMMV). I didn’t do TDD for this ( sue me 🙂 ).

  • Testing is not as pain-free as it seems. What layers to test and how to test them ?
    After all of this code is for separating components and making life easier in the long run by increasing our confidence in the code.
    If EF is already a UoW and Repository then what are doing building a layer on top of this ?
  • We end up having a service layer anyway since our application logic is often non trivial and needs to be kept separate. Then the real fun begins with tests.
  • Mocking EF is a pain since you don’t have an IDbContext available. Get your fake data in there , then mock the repository and then mock the repository methods, then if you want to mock something like Includes() you will have some real fun. The idea is not mock EF, but even checking anything that sits on top of it can become an exercise in itself.
    After writing the tests, I can confidently say that my confidence in my code had not increased the manner I had expected (you bet this confidence thing is funny business) . I often had the feeling of buyer’s remorse.
  • It felt like I was on the wrong path. What do we really care about ? We need to get our data in and out of our database. That is it. How does making an abstraction help? It does not help that much. We are working exactly with the IQueryable that EF gives us back. Some implementations also have methods like FindById(..) , SortBy(..) etc…
    We need to concentrate on queries in our application. Make queries first class in your application.
  • Was all of this useless ? No, not quite. Implementing the Repository Pattern this way is, the pattern is not useless.
    We leak our data access technology Entity Framework into the Application Layer. A UnitOfWork driven implementation is actually very useful.
    I have not given that a go in code but here is something to start with Repository Pattern

In conclusion, it was both fun and enriching to hit roadblocks and see why the developers are moving to and from the Repository Pattern. I was working with EF6 and that came with its fair share of headaches. I will blog about that soon. Stay hungry, stay awesome.

Team City – Recover Admin Password

The trials and tribulations of this world are beyond me. Sometimes, a simple gesture can save your life.
I lost(forgot, forgive my small brain it has to keep up with a day job) my TeamCity login details.
Searching online will lead you to this place Forgot Team City Password.

The solution there is mighty correct barring two things. One, your TeamCity data directory path is likely to C:\ProgramData\JetBrains\TeamCity\ on a windows box and not C:\users\foo\TeamCity. Second, you need to run CMD as admin.

Step 1 Stop TeamCity service ( either through services.msc or the bat files ).

(don't forget the admin part you will go bonkers trying to figure out what is happening)
and go to C:\TeamCity\webapps\ROOT\WEB-INF\lib

Step 3 type -cp server.jar; hsqldb.jar ChangePassword USERNAME PASSWORD C:\ProgramData\JetBrain\TeamCity

If all is successful then you password has been reset and you can login. Here’s hoping to a few hours saved for anyone who comes around to reading this.