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.
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.
Namespace: ErrorUnit.JsonSerializer
public class DbContextSerializer : JsonConverter
The DbContextSerializer type exposes the following members.
Name | Description | |
---|---|---|
DbContextSerializer | Initializes a new instance of the DbContextSerializer class |
Name | Description | |
---|---|---|
CanRead | (Inherited from JsonConverter.) | |
CanWrite | (Inherited from JsonConverter.) |
Name | Description | |
---|---|---|
CanConvert |
Indicates if DbContextSerializer can Convert the type
(Overrides JsonConverter.CanConvert(Type).) | |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetSchema | Obsolete. (Inherited from JsonConverter.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
ReadJson |
Read DbContextSerializer Json and convert into a Context with a deserialized backing database.
(Overrides JsonConverter.ReadJson(JsonReader, Type, Object, JsonSerializer).) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
WriteJson |
Writer DBContext as JSON.
(Overrides JsonConverter.WriteJson(JsonWriter, Object, JsonSerializer).) |
Name | Description | |
---|---|---|
dbFilePrefix |
The Prefix for ErrorUnit's databases it creates while unit testing
|