tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Upper-casing conventions as SQL likes it in Entity Framework 6

11 Nov 2014 Entity Framework

Before Entity Framework 6 was finalized I wrote posts ([1] and [2]) showing how with the help of conventions you can save yourself some tedious typing for databases following strictly SQL standard in respect to upper case and (not)quoting (see previous posts for details).

But that was pre-EF6 era and some API changed. In fact it’s now even way easier to do that.

Let’s do just simple “upper-casing” convention. Given I need to handle all columns and tables, even the ones generated, I need to use so-called store model conventions. These operate on the model in S-Space. The interface I’m going to use is IStoreModelConvention. This interface needs type parameter to describe on what element we’re going to operate. I’ll start with EdmProperty. This class represents column in S-Space. (Also I believe there’s a way from EntityType. But why to make it harder.) Whoever implements IStoreModelConvention interface must implement single method void Apply(T item, DbModel model). No problem.

public void Apply(EdmProperty item, DbModel model)
{
	item.Name = MakeUpperCase(item.Name);
}

For tables I need to dig into EntitySet type aka IStoreModelConvention<EntitySet>. Not a problem either.

public void Apply(EntitySet item, DbModel model)
{
	item.Table = MakeUpperCase(item.Table);
}

And that’s it. Either I can make it as two conventions or single one. I feel that this is single logical package so I made it one.

public class UpperCaseConvention : IStoreModelConvention<EntitySet>, IStoreModelConvention<EdmProperty>
{
	public void Apply(EntitySet item, DbModel model)
	{
		item.Table = MakeUpperCase(item.Table);
	}

	public void Apply(EdmProperty item, DbModel model)
	{
		item.Name = MakeUpperCase(item.Name);
	}

	protected virtual string MakeUpperCase(string s)
	{
		return s.ToUpperInvariant();
	}
}

I also made MakeUpperCase method virtual in case somebody would like to make slightly different implementation, simple subclassing it is.

With this it shouldn’t take long to create bunch of custom conventions (and combine these) to match naming conventions - like T_<tablename>, F_<columnname> or PropertyName -> PROPERTY_NAME.