Click or drag to resize
DbContextSerializer Class

The ErrorUnit DbContextSerializer works best with a code first context however it is possible to use a model first implementation.

The context class needs to persist for the whole lifetime of the call, so the easiest way is to use a singleton pattern backed by System.Web.HttpContext.Current.Items and a static property for when it is used by Unit Testing. Additionally ErrorUnit needs a way to override the connection so we add a additional constructor.

So for a DbContext named Northwind the code that would be changed/added is:
using System;
using System.Data.Entity;

public partial class Northwind : DbContext
   [Obsolete("Use static Instance property instead")]
   public Northwind()
       : base("name=Northwind")

   private static Northwind _Instance;
#pragma  warning disable CS0618 // Type or member is obsolete
   public static Northwind Instance
           return System.Web.HttpContext.Current != null
               ? (Northwind)(System.Web.HttpContext.Current.Items[typeof(Northwind).FullName] ?? (System.Web.HttpContext.Current.Items[typeof(Northwind).FullName] = new Northwind()))
               : _Instance ?? (_Instance = new Northwind());
#pragma  warning restore CS0618 // Type or member is obsolete

   [Obsolete("ErrorUnit uses this to build fake databases")]
   public Northwind(System.Data.Common.DbConnection existingConnection, bool contextOwnsConnection) : base(existingConnection, contextOwnsConnection) { }

    //Remaining Context Code...

And everywhere in code you would change new Northwind() to Northwind.Instance; and not use Northwind.Instance inside using statements

Note: If you are using a model first EF context you will have to create and keep up to date a SQL file that builds the database (aka a DDL); the DDL sql file must be created for SQL Compact Edition in the ErrorUnitTests directory of your Unit Test project and named after your context class name (in this example it would be named Northwind.sql). A DDL sql file can be created via this method and may need some adjusting to get it to work right.

Inheritance Hierarchy

Namespace: ErrorUnit.JsonSerializer
Assembly: ErrorUnit (in ErrorUnit.dll) Version: 1.3.0
public class DbContextSerializer : JsonConverter

The DbContextSerializer type exposes the following members.

Public methodDbContextSerializer
Initializes a new instance of the DbContextSerializer class
Public propertyCanRead (Inherited from JsonConverter.)
Public propertyCanWrite (Inherited from JsonConverter.)
Public methodCanConvert
Indicates if DbContextSerializer can Convert the type
(Overrides JsonConverter.CanConvert(Type).)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetSchema Obsolete. (Inherited from JsonConverter.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodReadJson
Read DbContextSerializer Json and convert into a Context with a deserialized backing database.
(Overrides JsonConverter.ReadJson(JsonReader, Type, Object, JsonSerializer).)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodWriteJson
Writer DBContext as JSON.
(Overrides JsonConverter.WriteJson(JsonWriter, Object, JsonSerializer).)
Public fieldStatic memberdbFilePrefix
The Prefix for ErrorUnit's databases it creates while unit testing
See Also