2021-07-22 12:29:26 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using RulesEngine.Models;
|
|
|
|
|
|
|
|
|
|
namespace DemoApp.EFDataExample
|
|
|
|
|
{
|
|
|
|
|
public class RulesEngineDemoContext : DbContext
|
|
|
|
|
{
|
|
|
|
|
public DbSet<WorkflowRules> WorkflowRules { get; set; }
|
|
|
|
|
public DbSet<ActionInfo> ActionInfos { get; set; }
|
|
|
|
|
|
|
|
|
|
public DbSet<RuleActions> RuleActions { get; set; }
|
|
|
|
|
public DbSet<Rule> Rules { get; set; }
|
|
|
|
|
public DbSet<ScopedParam> ScopedParams { get; set; }
|
|
|
|
|
|
|
|
|
|
public string DbPath { get; private set; }
|
|
|
|
|
|
|
|
|
|
public RulesEngineDemoContext()
|
|
|
|
|
{
|
|
|
|
|
var folder = Environment.SpecialFolder.LocalApplicationData;
|
|
|
|
|
var path = Environment.GetFolderPath(folder);
|
|
|
|
|
DbPath = $"{path}{System.IO.Path.DirectorySeparatorChar}RulesEngineDemo.db";
|
|
|
|
|
}
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
|
|
|
|
=> options.UseSqlite($"Data Source={DbPath}");
|
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<ActionInfo>()
|
|
|
|
|
.Property(b => b.Context)
|
|
|
|
|
.HasConversion(
|
2021-07-28 04:33:11 -04:00
|
|
|
|
v => JsonSerializer.Serialize(v, null),
|
|
|
|
|
v => JsonSerializer.Deserialize<Dictionary<string, object>>(v, null));
|
2021-07-22 12:29:26 -04:00
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<ActionInfo>()
|
|
|
|
|
.HasKey(k => k.Name);
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<ScopedParam>()
|
|
|
|
|
.HasKey(k => k.Name);
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<WorkflowRules>(entity => {
|
2021-07-28 04:33:11 -04:00
|
|
|
|
entity.HasKey(k => k.WorkflowName);
|
2021-07-22 12:29:26 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<RuleActions>(entity => {
|
|
|
|
|
entity.HasNoKey();
|
|
|
|
|
entity.HasOne(o => o.OnSuccess).WithMany();
|
|
|
|
|
entity.HasOne(o => o.OnFailure).WithMany();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<Rule>(entity => {
|
|
|
|
|
entity.HasKey(k => k.RuleName);
|
2021-07-28 04:33:11 -04:00
|
|
|
|
|
2021-07-22 12:29:26 -04:00
|
|
|
|
entity.Property(b => b.Properties)
|
|
|
|
|
.HasConversion(
|
2021-07-28 04:33:11 -04:00
|
|
|
|
v => JsonSerializer.Serialize(v, null),
|
|
|
|
|
v => JsonSerializer.Deserialize<Dictionary<string, object>>(v, null));
|
2021-07-22 12:29:26 -04:00
|
|
|
|
entity.Ignore(e => e.Actions);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<WorkflowRules>()
|
|
|
|
|
.Ignore(b => b.WorkflowRulesToInject);
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<Rule>()
|
|
|
|
|
.Ignore(b => b.WorkflowRulesToInject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|