---
title: "Entity Framework Connector"
description: "Lacuna PKI Entity Framework Connector, the optional PKI SDK package that stores CAdES revocation data and SDK logs in a database."
canonical: "https://docs.lacunasoftware.com/en-us/articles/pki-sdk/optional-packages/ef-connector"
locale: "en"
product: "PKI SDK"
last_updated: "2026-07-14"
---

# Entity Framework Connector

The package
[Lacuna PKI Entity Framework Connector](https://www.nuget.org/packages/Lacuna.Pki.EntityFrameworkConnector/)
enables the following integrations between the PKI SDK and
[Microsoft Entity Framework](https://docs.microsoft.com/en-us/ef/):

* Compress and decompress CAdES signatures storing the CRLs and certificates in
  a database
* Perform and validate CAdES signatures with revocation references but without
  revocation values (CAdES-X Type 1 or ICP-Brasil AD-RV) by storing the
  correspondent values on a database
* Send log messages generated by the SDK to a database

> **Note**
>
> This package assumes you are using the Code First workflow of Entity Framework.

## EntityFrameworkStore

The @Lacuna.Pki.EntityFrameworkConnector.EntityFrameworkStore class implements the @Lacuna.Pki.Stores.ISimpleStore
interface, which is used by the SDK whenever a storage is needed to store and/or retrieve objects, for
instance when compressing CAdES signatures (for more information, see [Optional nuget packages](https://docs.lacunasoftware.com/en-us/articles/pki-sdk/optional-packages)).

To use it, you must make your `DbContext` class implement the @Lacuna.Pki.EntityFrameworkConnector.IPkiStoreContext,
which basically means adding a `DbSet` with objects of type @Lacuna.Pki.EntityFrameworkConnector.PkiStoreObject to it:

```cs
public class MyDbContext : DbContext, IPkiStoreContext {
	...
	DbSet<PkiStoreObject> PkiStore { get; set; }
	...
}
```

If you're using automatic migrations, this change to your `DbContext` will cause
a new table named "LacunaPkiStore" to be created on the database. If you're
using code-based migrations, the necessary database changes will appear in the
next migration you create.

The next step is to instantiate an @Lacuna.Pki.EntityFrameworkConnector.EntityFrameworkStore passing your `DbContext`,
typically inside a `using` block:

```cs
using (var dbContext = new MyDbContext()) {
	...
	var store = new EntityFrameworkStore(dbContext);
	...
});
```

Once you have an instance of `EntityFrameworkStore` you can, for instance,
compress and decompress a CAdES signature:

```cs
byte[] precomputedSignature = ...; // any CAdES signature, not necessarily generated with the SDK
var compressedSignature = CadesSignatureCompression.Compress(precomputedSignature, store);
var decompressedSignature = CadesSignatureCompression.Decompress(compressedSignature, store);
// precomputedSignature and decompressedSignature will be the same
```

> **Warning**
>
> The `EntityFrameworkStore` class **will not call** the `SaveChanges()` method, you
> must call it yourself.

The `SaveChanges()` method is not called automatically in order to enable you to make the compression a part of a
transaction in your business logic. Therefore, make sure you call `SaveChanges()` afterwards:

```cs
using (var dbContext = new MyDbContext()) {
	...
	byte[] precomputedSignature = ...; // any CAdES signature, not necessarily generated with the SDK
	var store = new EntityFrameworkStore(dbContext);
	var compressedSignature = CadesSignatureCompression.Compress(precomputedSignature, store);
	...
	dbContext.SaveChanges();
});
```

## EntityFrameworkLogger

The @Lacuna.Pki.EntityFrameworkConnector.EntityFrameworkLogger class is used to send log messages generated by the
SDK to the database. To use it, you must make your `DbContext` class implement
the @Lacuna.Pki.EntityFrameworkConnector.IPkiLogContext, which basically means adding a `DbSet` of objects of
type @Lacuna.Pki.EntityFrameworkConnector.PkiLogEntry to it:

```cs
public class MyDbContext : DbContext, IPkiLogContext {
	...
	DbSet<PkiLogEntry> PkiLog { get; set; }
	...
}
```

If you're using automatic migrations, this change to your `DbContext` will cause
a new table named "LacunaPkiLog" to be created on the database. If you're
using code-based migrations, the necessary database changes will appear in the
next migration you create.

Then, all you need to do is call the method
`Lacuna.Pki.EntityFrameworkConnector.EntityFrameworkLogger.Configure(Lacuna.Pki.EntityFrameworkConnector.IPkiLogContext,LogLevels)`
of the `EntityFrameworkLogger` class passing an instance of your `DbContext`:

```cs
EntityFrameworkLogger.Configure(new MyDbContext());
```

> **Tip**
>
> The `EntityFrameworkLogger` class will call `SaveChanges()` on the given context
> whenever it deems necessary. Therefore, it is a good idea to pass to it a
> `DbContext` instance of its own.

## Dependency on Entity Framework

In order to maximise compatibility, the package depends on an old version the EntityFramework package, but we strongly
recommend that you install the latest 6.x version.

## Source code

This package is open source, hosted on
[BitBucket](https://bitbucket.org/Lacunas/pkientityframeworkconnector). Feel free to fork it if you need to make any
customizations.
