EF代码第一,具有多重关系的实体 [英] EF code first, Entities with multiple relations

查看:167
本文介绍了EF代码第一,具有多重关系的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里可以看到我想要存储在我的Sqlite数据库中的缩小的实体结构。我有一个图表,它包含一组 GraphElements 。我的边缘节点负载哪些都是不同的元素。

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.

对于深度优先搜索,每个节点需要知道其邻居节点。因此,我需要 NeigborNodes -List。对于其他功能,我还需要知道 ConnectedElements -List。

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; }
}

我的模型配置看起来像这样,当然不工作。 (我正在使用表类型(TPT)方法。)

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...
    }
}




我的问题:

(A)我更改了我的模型配置或我的实体以在我的数据库中存储 NeighborNodes

(A) How can I change my model configuration or my entities to store NeighborNodes in my database?

(B)我如何更改我的模型配置或我的实体在我的数据库中存储 ConnectedElements

(B) How can I change my model configuration or my entities to store ConnectedElements in my database?

感谢您的帮助!

推荐答案

我可以自己找到一个解决方案,我想简要解释一下:

I could find a solution myself which I would like to explain briefly:

这是一个多对多关系的问题,我不能首先认识到,因为我一直把它看作是一对多的关系问题。但是当你看到这样的时候,解决它很简单。在当前的情况下,您必须如下扩展模型。你必须告诉EF创建两个映射表。这样,您可以将Node和GridElements / NeighborNodes之间的关系存储在名为 NodeGridElement 的单独的DB映射表中 NodeNeighborNode

It is a matter of a "Many to Many" relationship what I was not able to realize in the first place because I always saw it as a "One to Many" relationship problem. But when you see it like this, it’s pretty simple to solve it. In the current case, you have to extend the model as follows. You have to tell EF to create two mapping tables. This way you can store the relationship between the Node and the GridElements/NeighborNodes in separate DB-mapping-tables called NodeGridElement respectiveNodeNeighborNode.

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);

        // Node
        modelBuilder.Entity<Node>()
            .HasMany(p => p.ConnectedElements)
            .WithMany()
            .Map(cs =>
            {
                cs.MapLeftKey("NodeId");
                cs.MapRightKey("GridElementId");
                cs.ToTable("NodeGridElement");
            });

        modelBuilder.Entity<Node>()
            .HasMany(p => p.NeighborNodes)
            .WithMany()
            .Map(cs =>
            {
                cs.MapLeftKey("NodeId");
                cs.MapRightKey("NeighborNodeId");
                cs.ToTable("NodeNeighborNode");
            });
    }
}

这篇关于EF代码第一,具有多重关系的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆