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.

Examples
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
   {
       get
       {
           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 https://msdn.microsoft.com/en-us/library/dd456815(v=vs.100).aspx and may need some adjusting to get it to work right.

Inheritance Hierarchy
SystemObject
  JsonConverter
    ErrorUnit.JsonSerializerDbContextSerializer

Namespace: ErrorUnit.JsonSerializer
Assembly: ErrorUnit (in ErrorUnit.dll) Version: 1.0.0.27669 (1.0.0.0)
Syntax
C#
public class DbContextSerializer : JsonConverter

The DbContextSerializer type exposes the following members.

Constructors
  NameDescription
Public methodDbContextSerializer
Initializes a new instance of the DbContextSerializer class
Top
Properties
  NameDescription
Public propertyCanRead (Inherited from JsonConverter.)
Public propertyCanWrite (Inherited from JsonConverter.)
Top
Methods
  NameDescription
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).)
Top
Fields
  NameDescription
Public fieldStatic memberdbFilePrefix
The Prefix for ErrorUnit's databases it creates while unit testing
Top
See Also