3

I would like to have the T4 template created by visual studio output my entities as IDbset rather than DbSet any idea how?

Thomas
  • 1,177
  • 1
  • 14
  • 26

1 Answers1

2

I assume you already have a t4 template the generates a DbContext. So just edit the T template for the context, which is probably named like Yourmodelname.Context.tt. There is a block like this in it (I'm using EF 5):

public string DbSet(EntitySet entitySet)
{
    return string.Format(
        CultureInfo.InvariantCulture,
        "{0} DbSet<{1}> {2} {{ get; set; }}",
        Accessibility.ForReadOnlyProperty(entitySet),
        _typeMapper.GetTypeName(entitySet.ElementType),
        _code.Escape(entitySet));
}

Now change "{0} DbSet<{1}> in "{0} IDbSet<{1}> and the context will have IDbSets.

Side note: if this is part of an operation to mock the context, you should know that that's pretty hopeless.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • What's your reasoning behind thinking mocking the context is hopeless? There seems to be a big differing of opinion on this – David Hayes Jul 23 '13 at 18:35
  • Thanks, I think there is some value in Mocking the context in simple cases but it's limited for sure. We're looking at a mixture of mocking the context and approval tests at the moment. I suspect we'll mostly end up with the latter. Certainly harder than it should be to unit test this stuff – David Hayes Jul 24 '13 at 01:24