Skip to content

Instantly share code, notes, and snippets.

/Context Secret

Created May 27, 2014
Embed
What would you like to do?
SO 23834083 Reference
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
namespace Data
{
public class StMonContext : StMonModelContainer
{
public StMonContext()
: base()
{
this.Configuration.ProxyCreationEnabled = false;
}
}
public partial class StMonModelContainer : DbContext
{
public StMonModelContainer()
: base("name=StMonModelContainer")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Block> Blocks { get; set; }
public DbSet<HistPoint> HistPoints { get; set; }
public DbSet<PerfMode> PerfModes { get; set; }
public DbSet<CalcPoint> CalcPoints { get; set; }
public DbSet<FilterCriteria> FilterCriterion { get; set; }
public DbSet<Filter> Filters { get; set; }
public DbSet<OperatorType> OperatorTypes { get; set; }
}
}
using System;
using System.Collections.Generic;
namespace Data
{
public partial class Block
{
public Block()
{
this.HistPoints = new HashSet<HistPoint>();
this.PerfModes = new HashSet<PerfMode>();
this.CalcPoints = new HashSet<CalcPoint>();
}
public int BlockId { get; set; }
public string Name { get; set; }
public virtual ICollection<HistPoint> HistPoints { get; set; }
public virtual ICollection<PerfMode> PerfModes { get; set; }
public virtual ICollection<CalcPoint> CalcPoints { get; set; }
}
public partial class CalcPoint
{
public CalcPoint()
{
this.PerfModes = new HashSet<PerfMode>();
this.Filters = new HashSet<Filter>();
}
public int CalcPointId { get; set; }
public string Expression { get; set; }
public string Name { get; set; }
public virtual Block Block { get; set; }
public virtual ICollection<PerfMode> PerfModes { get; set; }
public virtual ICollection<Filter> Filters { get; set; }
}
public partial class Filter
{
public Filter()
{
this.FilterCriterion = new HashSet<FilterCriteria>();
}
public int FilterId { get; set; }
public double AllowedDevPerc { get; set; }
public double Limit { get; set; }
public string Name { get; set; }
public virtual ICollection<FilterCriteria> FilterCriterion { get; set; }
public virtual OperatorType OperatorType { get; set; }
public virtual CalcPoint CalcPoint { get; set; }
}
public partial class FilterCriteria
{
public FilterCriteria()
{
this.Filters = new HashSet<Filter>();
}
public int FilterCriteriaId { get; set; }
public string Name { get; set; }
public virtual PerfMode PerfMode { get; set; }
public virtual ICollection<Filter> Filters { get; set; }
}
public partial class HistPoint
{
public int HistPointId { get; set; }
public string PointName { get; set; }
public virtual Block Block { get; set; }
}
public partial class OperatorType
{
public OperatorType()
{
this.Filters = new HashSet<Filter>();
}
public int OperatorTypeId { get; set; }
public string Label { get; set; }
public string Symbol { get; set; }
public virtual ICollection<Filter> Filters { get; set; }
}
public partial class PerfMode
{
public PerfMode()
{
this.CalcPoints = new HashSet<CalcPoint>();
this.FilterCriterion = new HashSet<FilterCriteria>();
}
public int PerfModeId { get; set; }
public string ModifiedBy { get; set; }
public System.DateTime ModifiedOn { get; set; }
public string Name { get; set; }
public string Notes { get; set; }
public virtual Block Block { get; set; }
public virtual ICollection<CalcPoint> CalcPoints { get; set; }
public virtual ICollection<FilterCriteria> FilterCriterion { get; set; }
}
}
[TestMethod()]
public void AddExistingCalcPointToPerfModeTest()
{
// Create a block with one calc point and a perf mode
using (StMonContext container = new StMonContext())
{
// Create a block
var block = new Block { Name = "TEST" };
// Create a perf mode
var perfMode = new PerfMode
{
Name = "Test Perf Mode",
Notes = "Created by unit test",
ModifiedBy = "Test",
ModifiedOn = DateTime.Now
};
// Create a calc point
var calcPoint = new CalcPoint { Name = "Test Calc Point", Expression = "A + B" };
// Add the perf mode to the block
block.PerfModes.Add(perfMode);
// Add the calc point to the block
block.CalcPoints.Add(calcPoint);
// Update
container.UpdateGraph(block, map => map
.OwnedCollection(b => b.CalcPoints)
.OwnedCollection(b => b.PerfModes));
// Save
container.SaveChanges();
}
Block detachedBlock = null;
PerfMode detachedPerfMode = null;
CalcPoint detachedCalcPoint = null;
// Get detached entities
using (StMonContext container = new StMonContext())
{
detachedBlock = container.Blocks.Include("PerfModes")
.Include("PerfModes.CalcPoints")
.Include("CalcPoints")
.Include("HistPoints")
.Single(b => b.Name == "TEST");
detachedPerfMode = detachedBlock.PerfModes.First();
detachedCalcPoint = detachedBlock.CalcPoints.First();
}
detachedBlock.PerfModes.First().CalcPoints.Add(detachedCalcPoint);
using (StMonContext container = new StMonContext())
{
// Try a full update
container.UpdateGraph(detachedBlock, map => map
.OwnedCollection(b => b.HistPoints)
.OwnedCollection(b => b.CalcPoints)
.OwnedCollection(b => b.PerfModes, with => with
.OwnedCollection(p => p.FilterCriterion, with2 => with2
.OwnedCollection(fc => fc.Filters, with3 => with3
.AssociatedEntity(f => f.OperatorType)
.AssociatedEntity(f => f.CalcPoint))))
.AssociatedCollection(p => p.CalcPoints)
);
// Save changes
container.SaveChanges();
}
// Check perf mode calc points count
using (StMonContext container = new StMonContext())
{
var block = container.Blocks.Include("PerfModes.CalcPoints").Single(b => b.Name == "TEST");
var perfMode = block.PerfModes.First();
Assert.AreEqual(1, perfMode.CalcPoints.Count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment