AppLabs.EntityFramework 1.0.8

dotnet add package AppLabs.EntityFramework --version 1.0.8                
NuGet\Install-Package AppLabs.EntityFramework -Version 1.0.8                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AppLabs.EntityFramework" Version="1.0.8" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AppLabs.EntityFramework --version 1.0.8                
#r "nuget: AppLabs.EntityFramework, 1.0.8"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AppLabs.EntityFramework as a Cake Addin
#addin nuget:?package=AppLabs.EntityFramework&version=1.0.8

// Install AppLabs.EntityFramework as a Cake Tool
#tool nuget:?package=AppLabs.EntityFramework&version=1.0.8                

AppLabs EntityFramework

El objetivo de está libreria es simplificar el inicio de cualquier proyecto con patrones ya conocidos.

Ver el proyecto AppLabs.EntityFramework.Test para ejemplos de su uso, en el repositorio del proyecto. Esta libreria solo genera un marco, es necesario agregar las librerias de EntityFrameworkCore que necesitas para tu base de datos.

Es necesario usar como destino la plaforma .NET Core 2.2 o superior para poderlo usarlo de lo contrario marcará problemas de compatibilidad.

Sqlite

Se agrego el soporte para utilizar Sqlite en la configuración, añade a tu proyecto las librerias de Microsoft.EntityFrameworkCore.Sqlite y Microsoft.EntityFrameworkCore.Design, para usar migraciones agrega también Microsoft.EntityFrameworkCore.Tools , verifica la clase TemporaryDbContextFactory.cs en la carpeta Helpers del proyecto de prueba.

La base de datos utilizada en el test, se encuentra en la carpeta DatabaseSqlite.

Verifica el proyecto de ejemplo para ver la configuración y uso.

Para mayor referencia verificar la documentación de microsoft para Sqlite para EF.

Inicio Rápido

Primero es necesario heredar la interfaz IDbContext tu contexto de datos e implementar la propiedad DataAccessConfiguration.

public class BitacoraContext : DbContext, IDbContext
{
	public IDataAccessConfiguration DataAccessConfiguration { get; set; }
	...

Despues hay que sobrescribir el método OnConfiguring, esto es totalmente necesario poder usar los objetos de la libreria

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
	base.OnConfiguring(optionsBuilder);

	if (!optionsBuilder.IsConfigured)
	{
		if (DataAccessConfiguration.UseOnMemory)
		{
			optionsBuilder.UseInMemoryDatabase("DbInMemory");
		}
		else if (DataAccessConfiguration.UseSqlite)
		{
			optionsBuilder.UseSqlite(DataAccessConfiguration.ConnectionString);
		}
		else if(DataAccessConfiguration.UseSqlServer)
		{
			optionsBuilder.UseSqlServer(
				DataAccessConfiguration.ConnectionString, options => options.EnableRetryOnFailure());
		} else
		{
			optionsBuilder.UseSqlServer(
				DataAccessConfiguration.ConnectionString, options => options.EnableRetryOnFailure());
		}
	}
}

Puedes omitir propiedades o incluso usar solo la cadena de conexión.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
	base.OnConfiguring(optionsBuilder);

	if (!optionsBuilder.IsConfigured)
	{
		optionsBuilder.UseSqlServer(DataAccessConfiguration.ConnectionString, options => options.EnableRetryOnFailure());
	}
}

El siguiente paso es añadir la configuracion (IDataAccessConfiguration).

Configuracion Directa

En el proyecto AppLabs.EntityFramework.Test se demuestra el uso directo. La IDataAccessConfiguration expone 4 propiedades para ayudar a la factoria a generar el DbContext de manera adecuada por medio del DatabaseFactory.

public string ConnectionString { get; set; }
public bool UseOnMemory { get; set; }
public bool UseSqlite { get; set; }
public bool UseSqlServer { get; set; }

Ejemplo usando Sqlite

[TestInitialize]
public void Initialize()
{	
	_uow = new UnitOfWork(new DbContextConfiguration<BitacoraContext>("Data Source=E:\\bitacora.db", true));
	InitProyectos();
	InitEtiquetas();
}

Ejemplo usando SqlServer (el constructor de DataAccessConfiguration establece por default SqlServer)

[TestInitialize]
public void Initialize()
{
	_uow = new UnitOfWork<BitacoraContext>(new DbContextConfiguration<BitacoraContext>("Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;"));
	InitProyectos();
	InitEtiquetas();
}

Configuracion Web

En el proyecto AppLabs.EntityFramework.Web.Demo se muestra como Para poder usar adecuadamente en un proyecto de .NET CORE 2.2+ primero seria necesario agregar la sección correspondiente en el appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "DataAccessConfiguration": {
    "ConnectionString": "Data Source=E:\\bitacora.db",
    "UseOnMemory": false,
    "UseSqlite": true,
    "UseSqlServer": false
  }
}

Y despues inyectar las dependiencias en startup.cs

 services.AddSingleton<IDbContextConfiguration<BitacoraContext>>(dc =>
                new DbContextConfiguration<BitacoraContext>($"{Configuration["DataAccessConfiguration:ConnectionString"]}",
                    bool.Parse(Configuration["DataAccessConfiguration:UseSqlite"])));

services.AddTransient<IUnitOfWork<BitacoraContext>, UnitOfWork<BitacoraContext>>();
services.AddTransient<IRepository<Proyecto>, Repository<Proyecto>>();
services.AddTransient<IRepository<Etiqueta>, Repository<Etiqueta>>();
services.AddTransient<IRepository<Entrada>, Repository<Entrada>>();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.8 661 8/15/2019
1.0.7 507 8/15/2019
1.0.6 492 8/15/2019
1.0.5 563 8/8/2019
1.0.4 528 8/8/2019
1.0.3 566 7/19/2019
1.0.2 627 2/9/2019
1.0.1 669 1/7/2019
1.0.0 696 1/5/2019