Faster code writing – experiences using MVC 4 and EF Code First

As part of my work at Zgura, I´m currently working on a MVC 4 solution using Windows Azure (and Azure SQL Database as well). We are developing to deploy on Azure websites. My supervisor insisted we use EF Code First, good advice. 2 weeks ago, I was developing for another project in MVC 4 as well, but in that case, I was using DB first (as I was using a legacy DB).

Now I can tell, using Code First has increased code development speed greatly, it is really helpful and time saving how you focus on classes, on the programming logic and let EF take charge of creating the entities at the DB. In the past 2 weeks since I started using Code First, I believe I´ve done the equivalent to at least 3 weeks with the previous methodology.

Add to this using view models we derivate from the classes for views, instead using ViewBags, at the beginning it may seem more complicated but let me tell you, it saves you a lot once you get used to how it works, I was using view models since before current project but it´s the necessary complement with Code First.

Please forgive my enthusiasm (well, I really like what I do). Will keep y´all posted on my experiences.


EF code first managing multiple foreign keys to a single entity

It is great to use code first, from our classes that represent entities, to be able to create the entities at the database and, through the navigation properties, allow us to get related data in our view models. It is great because we save time and make us to write a simpler code.

We can find ourselves in a case (not a rare one) in which we have a class with more than one foreign keys relating, both of them, to the same class (read entity). In this case we can use as follows, lets say, in our example we have the classes:

public class Employee

public int EmployeeId {get;set:}
public string Name {get,set;}
public virtual List<FactoryProcess> Shift1 {get; set}
public virtual List<FactoryProcess> Shift2 {get; set;}

public class FactoryProcess
public int ProcessId {get;set;}
public int Operator1Id { get; set; }
public virtual Employee Operator1 { get; set; }
public int Operator2Id { get; set; }
public virtual Employee Operator2 { get; set; }


And at the context class (this will do the “magic” completing in appropriated way the necessary relations between entities):

protected override void OnModelCreating(DbModelBuilder modelBuilder)
                .HasRequired(m => m.Operator1)
                .WithMany(t => t.Shift1)
                .HasForeignKey(m => m.Operator1Id)
                .HasRequired(m => m.Operator2)
                .WithMany(t => t.Shift2)
                .HasForeignKey(m => m.Operator2Id);       


Changing Development Perspectives – Entity Framework and Code First

It is until the last 2 projects I’ve been working that I started to use code first. This is the first article of a series I plan to write on my experiences on this topic.

I have to recognize my friend and supervisor at my new job at Zgura for promoting its use in our development projects. Now I have had the opportunity to work with it, I’ll argue for its use, even if I already have an existent database, no doubts when starting with a new one.

It is great to use it with MVC 4. You just have to create your Models folder (we usually keep that in another project, at the VS solution), add classes, decorate properties as necessary, enable migrations and update database.

The concept works great as, after the initial learning curve, it helps to increase greatly speed of development, more if it is combined with view models.

Tip: if you keep the models in a separated project, remember to let the system know what is the name of the project containing the models to be used by code first migrations:

Enable-Migrations -ProjectName “MyProjectModelName” -StartUpProjectName “MyStartupProjectName”

These instructions for migrations are executed from the menu Tools/Library Package Manager/Package Manager Console in Visual Studio 2012.