Tag Archives: asp.net

Entity Framework – When to use what

In Entity Framework there are three ways of utilising the ORM, one enables you to get straight into coding (code-first), one enables you to rip a database into a new model (database-first) and the final one enables you to create a model independent of the database or your final code (model-first).

But, when should I use each of these methods?

Each method has its pro’s and con’s, and personally, I don’t really use code-first that often as it lends itself to a build where everything has been fully architected beforehand, and all you’re doing is building to spec. Something I rarely encounter, as the initial green field development is often a very agile process, especially if you’re utilising a TDD/BDD development cycle.

So what scenario would you legitimately use Code-First?

Say you have a very small micro-service to build, such as an auditing service, and you already know the database fields, and possibly the service will only know its connection at runtime. Code-First is an ideal solution, as it enables you to quickly knock out the code, leaving the spinning up and implementation of the database to the EF settings in config. The main drawback I find of Code-First is that if you’re database schema is not set in stone, a rebuild of your EF model will necessitate a destruction of the database. You can create a custom upgrade path, but this is rarely done. So, if you have a unchanging model, for a small data footprint, code-first is great.

Code-First is also great for proof-of-concept builds that may be knocked out in a day to show a particular prospect of development.

Database-First is obviously good for where your development is bound to a database schema which already exists and is quite complex. You can just use the EF designer in Visual studio to generate the model and get up and running very quickly. A database schema change will mean that the EF model will need to be recreated, but its generally no big deal as the database will be keeping its data integrity due to it being developed in its own development domain by DBAs or other colleagues.

Model-First would generally be used to map out a logical structure of the data which bridges both the system model and the database model. Say you wish to use a different paradigm of data design in the database to your model (flat-file DB with a relational ORM). It could also be the case that you are tasked with a data-design task where you need to develop a schema that satisfies the requirements of the database team and the architect, utilising a colourful database like Oracle or MySQL to fit.

I hope this helps your decide the approach to use when implementing Entity Framework in your work.

Take care

Coming soon to your company?


Available from : September 2016

I’m going to update this post with my availability, so if you want to chat, give me a call. My resume and contact details are on the main page at www.neilhighley.com. Mention you saw this post and I’ll call right back!

My current role involves mainly desktop development and Industrial Hardware controllers. It’s a little different from what I normally do, but it seemed a good fit for my current interests in IOT and they’re actively using TDD and BDD for their production pipeline.

Online and Offline Software Developer available for C#, HTML, CSS, JavaScript roles from the start of November. I have almost 20 years commercial web experience and consider myself multistack, having worked across the multiple industries in a lot of positions throughout software development and operations.

Blog address: http://blog.neilhighley.com

Github repo: http://github.com/neilhighley

Main Site: http://www.neilhighley.com

Devpost: http://devpost.com/neilhighley

Daily Rate available on request.

ASP.NET 4 / 4.5 / C# / LINQ / jQuery / Javascript / REST /  XML / Visual Studio 2013 / Resharper / Git / MSSQL  / IIS8 / Windows Server / MSSQL /  CSS3 / HTML / TDD / KnockoutJS / Angular 1.0 / Azure

PHP / WordPress / MySQL

Connecting Apache Cassandra v2.011 to Windows Communication Foundation

Cassandra is the current de facto standard in flat, scalable, cloud based database applications and as such usually only exists in AWS or linux clusters.
Fortunately, you can install Cassandra on windows in a single node configuration. For more info on the configurations available in a distributed install, see Planet Cassandra.
Previously I have worked on SOLR, which struck me as a similar construct to the node structure of Cassandra, specifically with the lack of master slave that you would normally have found in traditional clusters.

By avoiding a master slave format, Cassandra allows an elastic structure to cover the database records, akin to a RAID striping,  and does a great job when you have geo-spaced clusters allowing for quick responses from the database at a global scale.

Cassandra has several connectors,  covering all the major server side languages, including Javascript(nodejs), Java, Python, Ruby, C#.

Here I will show you how to connect a C# WCF service to Cassandra to retrieve records.

Firstly, install Cassandra, taking special care with prerequisites.
Go into the cassandra query language shell(CQL, and it probably has a python icon on it)..

Create a new keyspace, which allows you to add tables.

create keyspace casswcfexample with replication={'class':'SimpleStrategy', 'replication_factor':1};

Add a new table to your keyspace.

use casswcfexample;
create table exampletable(tid int primary key, title varchar, description varchar);

You can press enter for a new line in CQL, and it will continue, and won’t submit the statement until you add a semicolon

Add data to your keyspace’s exampletable

insert into exampletable(tid, title, description) values(1,'first title', 'description');
insert into exampletable(tid, title, description) values(2,'second title', 'another description');

Verify the data in your keyspace:table

select * from exampletable;

OK, now we have a running cassandra instance with data in it, let’s hook it up to a new, empty, WCF project.


As a rule, I always have my data layer as a separate project, to allow me to reuse it.
Create a new Solution, and add a WCF Application, leave it named as WCFService1.
Create a new Class library project and , call it CassLibrary.

In the CassLibrary project, import the DataStax nuget package for the cassandra connector.

Create an object to hold our result rows

namespace CassLibrary
 public class DTOExampleTable
 public int Id { get; set; }
 public string Title { get; set; }
 public string Description { get; set; }

Create an Extension to convert row to object

namespace CassLibrary
 public static class DataExtensions
 public static IEnumerable<DTOExampleTable> ToExampleTables(this RowSet rows)
 return rows.Select(r => new DTOExampleTable()
     Id = int.Parse(r["tid"].ToString()),
     Title = r["title"].ToString(),
     Description = r["description"].ToString()


Create a Simple Data class to hold the session connection and the retrieval of our data

namespace CassLibrary
 public class Data
 private string _ks;
 private string _cp;

 public Data(string contactPoint, string keyspace)
 _cp = contactPoint;
 _ks = keyspace;

 private ISession GetSession()
 Cluster cluster = Cluster.Builder().AddContactPoint(_cp).Build();
 ISession session = cluster.Connect(_ks);
 return session;

 public IEnumerable<DTOExampleTable> GetExampleRows()
 var s = GetSession();
 RowSet result = s.Execute("select * from exampletable");
 return result.ToExampleTables();


This is just a simple way of showing the data access. The cassandra plugin has a representation of LINQ also, but I’ll not go into it here.

Now, go back to the WCFService1 project, and add a new method to the Service Contract interface.

 public interface IService1
 IEnumerable<DTOExampleTable> GetExampleData();

And in the actual service, add the corresponding method. We’ll add some test data, then verify the service is set up correctly first.

 public class Service1 : IService1
 public IEnumerable<DTOExampleTable> GetExampleData()
 var dto = new List<DTOExampleTable>();

 dto = new List<DTOExampleTable>()
 new DTOExampleTable()
 Id = 999,
 Title = "Just a test for WCF",
 Description = "WCF Desc test"
 new DTOExampleTable()
 Id = 99,
 Title = "Just another test for WCF",
 Description = "WCF Desc test 2"


 return dto;


Right click the WCF project and start debugging

If the WCFTestClient doesn’t fire up, go to the Visual studio command prompt and type in


Then attach your service URL to the client.

Once attached, select the GetExampleData Method in your service and invoke. You should see the two test items we added above.

Now that we know the service is fine, remove the test items from the service method, replacing them with the Data class in your CassLibrary.

 public class Service1 : IService1
 public IEnumerable<DTOExampleTable> GetExampleData()
 return new Data("", "casswcfexample").GetExampleRows();

Rebuild your service, and re-invoke it through WCFTestClient. You should see your Cassandra data.

The Cassandra client has DBContext and LINQ built in, so the example above should in no way be utilised in a real-life programming situation.

If i get a few hits on this page, I’ll post a more complete example, showing off the actual speed of Cassandra (a large cluster can serve hundreds of thousand of records a second). Cassandras only real competitor is MongoDB, which seemingly tops out at 20 nodes in a cluster.

Its this throughput and stability in scaling that enabled Cassandra to be used in the Large Hadron Collider to store data as it came into one of the detectors.

Planet Cassandra : Companies Using Cassandra