如何使用 entity framework 实现瞬态故障处理模式
How to implement transient fault handling pattern with entity framework
我需要实现以下模式:
https://msdn.microsoft.com/en-us/library/dn589799.aspx
我在一本书上读到如下内容:
If you are using Entity Framework 6 (EF 6), the retry logic for
transient faults is built in to the framework. When your EF 6 model is
in your project, you need to create a new class that derives from
DbConfiguration and customizes the execution strategy in the
constructor. EF 6 will look for classes that derive from
DbConfiguration in your project and use them to provide resiliency. To
set this, add a new Class file to your project and add using
statements for System.Data.Entity and System.Data.Entity.SqlServer.
代码如下:
public class EFConfiguration : DbConfiguration
{
public EFConfiguration()
{
this.SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
}
}
但是我不确定如何在我的代码中实现它:
public class AppDataContext : DbContext
{
public AppDataContext() : base("AppDataContext")
{
}
public DbSet<Module> Modulos { get; set; }
public DbSet<Empresa> Empresas { get; set; }
public DbSet<Entidad> Entidades { get; set; }
public DbSet<Propiedad> Propiedades { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
public class AppDataContextInitializer : System.Data.Entity.DropCreateDatabaseAlways<AppDataContext>
{
protected override void Seed(AppDataContext context)
{
#region Seed Modules
context.Modulos.Add(new Module() { Id = 1, ModuleName = "Contabilidad", FontAwesomeClass = "fa-ambulance" });
context.Modulos.Add(new Module() { Id = 2, ModuleName = "Recursos Humanos", FontAwesomeClass = "fa-heartbeat" });
context.Modulos.Add(new Module() { Id = 3, ModuleName = "Inventario", FontAwesomeClass = "fa-anchor" });
context.Modulos.Add(new Module() { Id = 3, ModuleName = "Produccion", FontAwesomeClass = "fa-binoculars" });
context.Modulos.Add(new Module() { Id = 3, ModuleName = "Ventas", FontAwesomeClass = "fa-coffee" });
context.Modulos.Add(new Module() { Id = 3, ModuleName = "Compras", FontAwesomeClass = "fa-calendar-o" });
context.Modulos.Add(new Module() { Id = 3, ModuleName = "Cotizaciones", FontAwesomeClass = "fa-building" });
#endregion
#region Seed Empresas
context.Empresas.Add(new Empresa() { Id = 1,
Nombre = "XYA",
NIT = "900854343",
NombreRepresentanteLegal = "Carla Peresz",
TelefonoRepresentanteLegal = "123",
NombreContacto = " Esteban Andres",
TelefonoContacto = "123"
});
#endregion
#region Seed Entidades
context.Entidades.Add(new Entidad()
{
Id = 1,
Nombre = "Empresa",
Propiedades = new List<Propiedad>()
{
new Propiedad()
{
Codigo="01",
Nombre="Twitter",
TipoDeDatos="Texto"
}
}
});
#endregion
#region Seed Propiedad
#endregion
base.Seed(context);
}
}
在这里,您对弹性和重试一致性有点困惑。
Connection Resiliency refers to the ability for EF to automatically retry any commands that fail due to these connection(Network unavailability) breaks.
我希望你可能不想干涉这个,因为 EF 做得很好。从您提到的 MSDN article 来看,您似乎想为您的实体使用缓存模式。
好吧,我可以建议您尝试实现 UnitOfWork 以将上下文 CRUD 操作保持在一个位置。
然后您可以根据操作请求的类型在UnitOfWork class 和fetch/create/invalidate 缓存项中实现您的缓存。
Make sure that cache-aside pattern doesn't give you 100% consistency. E.g. if an external operation changes the data in the DB then Cache would not be aware about that change.
我需要实现以下模式: https://msdn.microsoft.com/en-us/library/dn589799.aspx
我在一本书上读到如下内容:
If you are using Entity Framework 6 (EF 6), the retry logic for transient faults is built in to the framework. When your EF 6 model is in your project, you need to create a new class that derives from DbConfiguration and customizes the execution strategy in the constructor. EF 6 will look for classes that derive from DbConfiguration in your project and use them to provide resiliency. To set this, add a new Class file to your project and add using statements for System.Data.Entity and System.Data.Entity.SqlServer.
代码如下:
public class EFConfiguration : DbConfiguration
{
public EFConfiguration()
{
this.SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
}
}
但是我不确定如何在我的代码中实现它:
public class AppDataContext : DbContext
{
public AppDataContext() : base("AppDataContext")
{
}
public DbSet<Module> Modulos { get; set; }
public DbSet<Empresa> Empresas { get; set; }
public DbSet<Entidad> Entidades { get; set; }
public DbSet<Propiedad> Propiedades { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
public class AppDataContextInitializer : System.Data.Entity.DropCreateDatabaseAlways<AppDataContext>
{
protected override void Seed(AppDataContext context)
{
#region Seed Modules
context.Modulos.Add(new Module() { Id = 1, ModuleName = "Contabilidad", FontAwesomeClass = "fa-ambulance" });
context.Modulos.Add(new Module() { Id = 2, ModuleName = "Recursos Humanos", FontAwesomeClass = "fa-heartbeat" });
context.Modulos.Add(new Module() { Id = 3, ModuleName = "Inventario", FontAwesomeClass = "fa-anchor" });
context.Modulos.Add(new Module() { Id = 3, ModuleName = "Produccion", FontAwesomeClass = "fa-binoculars" });
context.Modulos.Add(new Module() { Id = 3, ModuleName = "Ventas", FontAwesomeClass = "fa-coffee" });
context.Modulos.Add(new Module() { Id = 3, ModuleName = "Compras", FontAwesomeClass = "fa-calendar-o" });
context.Modulos.Add(new Module() { Id = 3, ModuleName = "Cotizaciones", FontAwesomeClass = "fa-building" });
#endregion
#region Seed Empresas
context.Empresas.Add(new Empresa() { Id = 1,
Nombre = "XYA",
NIT = "900854343",
NombreRepresentanteLegal = "Carla Peresz",
TelefonoRepresentanteLegal = "123",
NombreContacto = " Esteban Andres",
TelefonoContacto = "123"
});
#endregion
#region Seed Entidades
context.Entidades.Add(new Entidad()
{
Id = 1,
Nombre = "Empresa",
Propiedades = new List<Propiedad>()
{
new Propiedad()
{
Codigo="01",
Nombre="Twitter",
TipoDeDatos="Texto"
}
}
});
#endregion
#region Seed Propiedad
#endregion
base.Seed(context);
}
}
在这里,您对弹性和重试一致性有点困惑。
Connection Resiliency refers to the ability for EF to automatically retry any commands that fail due to these connection(Network unavailability) breaks.
我希望你可能不想干涉这个,因为 EF 做得很好。从您提到的 MSDN article 来看,您似乎想为您的实体使用缓存模式。
好吧,我可以建议您尝试实现 UnitOfWork 以将上下文 CRUD 操作保持在一个位置。
然后您可以根据操作请求的类型在UnitOfWork class 和fetch/create/invalidate 缓存项中实现您的缓存。
Make sure that cache-aside pattern doesn't give you 100% consistency. E.g. if an external operation changes the data in the DB then Cache would not be aware about that change.