Base class in Entity Framework 6?

I have many common methods in my program that take some generated object as a parameter. So, methods like:

public void DoHerpDerp<EntityType>()

While this is normal and completing the task, users of my methods can still pass everything they need as a general parameter (and application crash). I want to strictly restrict them to objects of generated objects (I use the Database First approach). I want to write something like:

public void DoHerpDerp<EntityType>() where EntityType : BaseEntity

Is there a class like BaseEntity, and if it's not the same, how do I get around this? And no, I'm not going to write 200 partial classes that implement the interface.

+3
source share
1 answer

You can change the generation of objects by adapting the T4 template.

T4 (, Model.tt) , . "partial class MyEntity":

public string EntityClassOpening(EntityType entity)
{
    return string.Format(
        CultureInfo.InvariantCulture,
        "{0} {1}partial class {2}{3}",
        Accessibility.ForType(entity),
        _code.SpaceAfter(_code.AbstractOption(entity)),
        _code.Escape(entity),
        _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
}

public string EntityClassOpening(EntityType entity)
{
    return string.Format(
        CultureInfo.InvariantCulture,
        "{0} {1}partial class {2}{3}{4}",
        Accessibility.ForType(entity),
        _code.SpaceAfter(_code.AbstractOption(entity)),
        _code.Escape(entity),
        _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)),
        string.IsNullOrEmpty(_code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType))) ? _code.StringBefore(" : ", "BaseClass") : "");
}

, , BaseClass, .

+6

All Articles