Show / Hide Table of Contents
Edit on GitHub

Entity Framework Connector

The package Lacuna PKI Entity Framework Connector enables the following integrations between the PKI SDK and Microsoft Entity Framework:

  • 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 EntityFrameworkStore class implements the 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).

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

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 EntityFrameworkStore passing your DbContext, typically inside a using block:

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:

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:

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 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 IPkiLogContext, which basically means adding a DbSet of objects of type PkiLogEntry to it:

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 Configure(IPkiLogContext, LogLevels) of the EntityFrameworkLogger class passing an instance of your DbContext:

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. Feel free to fork it if you need to make any customizations.

Back to top Copyright © 2015-2020 Lacuna Software