Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF代码优先:如何在一个表中保存两个实体的数据?_C#_Entity Framework - Fatal编程技术网

C# EF代码优先:如何在一个表中保存两个实体的数据?

C# EF代码优先:如何在一个表中保存两个实体的数据?,c#,entity-framework,C#,Entity Framework,我有两个实体称为ControllerBlock和ControllerAction 查看GetControllerBlocks函数,您就可以理解我是如何填充两个类的数据的,比如主详细信息。如何使用EF将这两个实体的数据存储到一个表中 表结构类 ControllerName ActionName -------------- ------------ Home Index Home DasBoard Home

我有两个实体称为ControllerBlock和ControllerAction

查看GetControllerBlocks函数,您就可以理解我是如何填充两个类的数据的,比如主详细信息。如何使用EF将这两个实体的数据存储到一个表中

表结构类

ControllerName    ActionName
--------------    ------------
Home                Index
Home                DasBoard
Home                Chart
HR                  Leave
HR                  Loan
Payroll             view
Payroll             Edit
Payroll             Process
我的示例代码:

public class ControllerBlock
{
    public string ControllerName { get; set; }
    public List<ControllerAction> ControllerActions { get; set; }
}

public class ControllerAction
{
    public string ActionName { get; set; }
}

public List<ControllerBlock> GetControllerBlocks()
{
    // Set ActionActive herem, if necessary
    List<ControllerAction> homeActions = new List<ControllerAction>() {
        new ControllerAction { ActionName = "Index" },
        new ControllerAction {  ActionName = "DashBoard" },
        new ControllerAction { ActionName = "Chart" }
    };

    List<ControllerAction> hrActions = new List<ControllerAction>() {
        new ControllerAction { ActionName = "Leave" },
        new ControllerAction { ActionName = "Loan" },
        new ControllerAction { ActionName = "NoticeBoard" }
    };

    List<ControllerAction> payRollActions = new List<ControllerAction>() {
        new ControllerAction { ActionName = "View" },
        new ControllerAction {  ActionName = "Edit" },
        new ControllerAction { ActionName = "Process" }
    };

    List<ControllerBlock> actionBlocks = new List<ControllerBlock>()
    {
        new ControllerBlock(){ControllerName = "Home", ControllerActions = homeActions},
        new ControllerBlock(){ControllerName = "HR",  ControllerActions =  hrActions},
        new ControllerBlock(){ControllerName = "PayRoll",  ControllerActions =  payRollActions}
    };

    return actionBlocks;
}

我首先使用EF代码,所以当我有两个实体时,建议在一个表中插入数据的最佳方法。如果可能,添加一些代码提示,用于将数据从两个类插入到一个表中。谢谢

您基本上希望将数据展平为一个序列。这可以通过使用

基于表结构,假设实体如下所示

public class Entity {
    public string ControllerName { get; set; } 
    public string ActionName { get; set; }
}
基于所提供的模型

var actionBlocks = GetControllerBlocks();
//Flatten the collection of block.
List<Entity> controllerActions = actionBlocks
    .SelectMany(b => b.ControllerActions
        .Select(a => new Entity { 
            ControllerName = b.ControllerName, 
            ActionName = a.ActionName 
         })
     )
    .ToList();

从那里,您应该能够将所有内容保存到一个表中。

您基本上希望将数据展平为一个序列。这可以通过使用

基于表结构,假设实体如下所示

public class Entity {
    public string ControllerName { get; set; } 
    public string ActionName { get; set; }
}
基于所提供的模型

var actionBlocks = GetControllerBlocks();
//Flatten the collection of block.
List<Entity> controllerActions = actionBlocks
    .SelectMany(b => b.ControllerActions
        .Select(a => new Entity { 
            ControllerName = b.ControllerName, 
            ActionName = a.ActionName 
         })
     )
    .ToList();

从那时起,您应该能够将所有内容保存到一个表中。

您不能,甚至不应该尝试这样做。我听说这可以通过表拆分来完成。您不能,甚至不应该尝试这样做。我听说这可以通过表拆分来完成。两个实体数据不可能保存在一个表中?据我所知,这并不是什么问题请告诉我你为什么先选择多个然后再选择?请用理性来指导我。@MonojitSarkar,仔细阅读这里的SelectMany,比我解释得更好。他们甚至有一个例子说明了如何使用它以及为什么要使用它。两个实体数据不可能保存在一个表中?据我所知,这不是EF可以告诉我为什么要先选择多个,然后再选择?请用理性来指导我。@MonojitSarkar,仔细阅读这里的SelectMany,比我解释得更好。他们甚至有一个例子说明了如何以及为什么使用它。