Here you can see my reduced entity structure which I would like to store in my Sqlite database. I've a Graph
which holds a Set of GraphElements
. My Graph
consists of Edges
, Nodes
and Loads
which are all different Elements.
For a deep-first-search for example each node needs to know its neighbor nodes. Therefore I need the NeigborNodes
-List. For other functionalities I need also to know the ConnectedElements
-List.
class Graph
{
public int Id { get; set; }
public string Name { get; set; }
public virtual List<GraphElement> GraphElements { get; set; }
}
[Table("GraphElements")]
abstract class GraphElement
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Graph Graph { get; set; }
}
[Table("Nodes")]
class Node : GraphElement
{
public virtual List<Node> NeighborNodes { get; set; }
public virtual List<GraphElement> ConnectedElements { get; set; }
}
[Table("Edges")]
class Edge : GraphElement
{
public virtual Node From { get; set; }
public virtual Node To { get; set; }
}
[Table("Loads")]
class Load : GraphElement
{
public virtual Node From { get; set; }
}
My model configuration looks at the moment like this and is of course not working. (I'm working with the Table per Type (TPT) approach.)
public class ModelConfiguration
{
private static void ConfigureGridDataCollectionEntity(DbModelBuilder modelBuilder)
{
// Graph
modelBuilder.Entity<Graph>().ToTable("Base.GraphTable")
.HasRequired(p => p.GraphElements)
.WithMany()
.WillCascadeOnDelete(true);
// GraphElement
modelBuilder.Entity<GraphElement>()
.HasRequired(p => p.Graph)
.WithMany(graph => graph.GraphElements)
.WillCascadeOnDelete(false);
// Edge
modelBuilder.Entity<Edge>()
.HasOptional(p => p.From)
.WithMany(node => node.ConnectedElements) // Convertion error
.WillCascadeOnDelete(false);
modelBuilder.Entity<Edge>()
.HasOptional(p => p.To)
.WithMany(node => node.ConnectedElements) // Convertion error
.WillCascadeOnDelete(flase);
// Load
modelBuilder.Entity<Load>()
.HasOptional(p => p.From)
.WithMany(node => node.ConnectedElements) // Convertion error
.WillCascadeOnDelete(false);
// Node
// No idea at all...
}
}
My question:
(A) How can I change my model configuration or my entities to store
NeighborNodes
in my database?(B) How can I change my model configuration or my entities to store
ConnectedElements
in my database?
Thank you for the help!