Skip to content

Commit

Permalink
Merge pull request #65 from rwasef1830/doc_ef6_interceptor_example
Browse files Browse the repository at this point in the history
Document the possibility of using EF6 interceptors
  • Loading branch information
roji authored Feb 16, 2017
2 parents 2041bfd + db877ea commit ce6c4d7
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,61 @@ In PostgreSQL, this implicitly uses `template1` as the template - anything exist
be copied to your new database. If you wish to change the database used as a template, you can specify
the `EF Template Database` connection string parameter. For more info see the
[PostgreSQL docs](https://www.postgresql.org/docs/current/static/sql-createdatabase.html).

## Customizing DataReader Behavior ##

You can use [an Entity Framework 6 IDbCommandInterceptor](https://msdn.microsoft.com/en-us/library/dn469464(v=vs.113).aspx) to wrap the `DataReader` instance returned by Npgsql when Entity Framework executes queries. This is possible using a ```DbConfiguration``` class.

Example use cases:
- Forcing all returned ```DateTime``` and ```DateTimeOffset``` values to be in the UTC timezone.
- Preventing accidental insertion of DateTime values having ```DateTimeKind.Unspecified```.
- Forcing all postgres date/time types to be returned to Entity Framework as ```DateTimeOffset```.

```c#
[DbConfigurationType(typeof(AppDbContextConfiguration))]
public class AppDbContext : DbContext
{
// ...
}

public class AppDbContextConfiguration : DbConfiguration
{
public AppDbContextConfiguration()
{
this.AddInterceptor(new MyEntityFrameworkInterceptor());
}
}

class MyEntityFrameworkInterceptor : DbCommandInterceptor
{
public override void ReaderExecuted(
DbCommand command,
DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
if (interceptionContext.Result == null) return;
interceptionContext.Result = new WrappingDbDataReader(interceptionContext.Result);
}

public override void ScalarExecuted(
DbCommand command,
DbCommandInterceptionContext<object> interceptionContext)
{
interceptionContext.Result = ModifyReturnValues(interceptionContext.Result);
}

static object ModifyReturnValues(object result)
{
// Transform and then
return result;
}
}

class WrappingDbDataReader : DbDataReader, IDataReader
{
// Wrap an existing DbDataReader, proxy all calls to the underlying instance,
// modify return values and/or parameters as needed...
public WrappingDbDataReader(DbDataReader reader)
{
}
}
```

0 comments on commit ce6c4d7

Please sign in to comment.