Skip to main content

EF - DataContext Attributes

When creating a provider-scoped data context (one for a specific backend), you need to decorate the type with a DataContextAttribute.

This allows application code, to easily identify the provider-specific data contexts, to use for a particular backend choice.

And, it allows other code to identify the provider and associated configuration of each type, and whatever other properties we may need to include at a later time.

NOTE: There is no need to add the attribute to a base db context type, or an app domain context type (one with DbSets).
This only applies to the provider-specific data context types.

Attribute Strings

Here are the standard provider choice strings:

Backend Provider Name ShortName
PostgreSQL PostGreSQL postgres
SQL Server SQLServer sqlserver
In Memory InMemory inmem
SQLite SQLite sqlite

Examples

Here's an example of how to decorate a Postgres data context:

[DataContext("PostGreSQL", "postgres", "PostGresDatabase")]
public class Postgres_TestDbContext : TestDbContext
{
}

Here's an example of how to decorate a SQL Server data context:

[DataContext("SQLServer", "sqlserver", "sqlserverDatabase")]
public class SQLServer_TestDbContext : TestDbContext
{
}

Here's an example of how to decorate a SQLite data context:

[DataContext("SQLite", "sqlite", "SQLiteDatabase")]
public class SQLite_TestDbContext : TestDbContext
{
}

Here's an example of how to decorate a Postgres data context:

[DataContext("InMemory", "inmem", "InMemoryDatabase")]
public class InMem_TestDbContext : TestDbContext
{
}