简体   繁体   English

如何从两个表中获取数据并将其保存在Net Core Entity Framework的DTO中?

[英]How to get data from two tables and save it in DTO in Net Core Entity Framework?

I just explain this in example, say I have two tables, field and modulefield 我只是在示例中进行解释,说我有两个表,字段和模块字段

field
id     name          ismodule
1      Firstname     1
2      Lastname      0

modulefield
id     moduleid      fieldid
1      22            1

That is a simple representation of my database. 那是我的数据库的简单表示。 Now in EF, how can I get a row with these columns 现在在EF中,如何获得这些列的行

modulefield.moduleid     field.id     field.name
22                       1            Firstname

Basically, I just want to get the modulefield.moduleid if the field.ismodule is true. 基本上,如果field.ismodule为true,我只想获取modulefield.moduleid。

The equivalent sql script I think for this is 我认为与此等效的SQL脚本是

SELECT b.submoduleid, a.* FROM field a, modulefield b;

My current code is this(snippet) 我当前的代码是这个(片段)

   var result = new List<FieldDTO>();
   var fields = await _repo.GetAsync<Field>();
   result = _mapper.Map<List<FieldDTO>>(fields);

I assume there could be more then one Module per Field. 我认为每个字段可能会有一个以上的模块。 Query: 查询:

SELECT mf.submoduleid, f.id, f.name 
FROM field AS f, modulefield AS mf
WHERE f.ismodule = 1 AND
    f.id = mf.fieldid

EF Linq expression: EF Linq表达式:

List<FieldDTO> result = _context.Fields
    .Include("ModuleField")
    .Where(f => f.ismodule == true)
    .Select(f => new FieldDTO(){
        ModuleFieldModuleId = f.ModuleField.moduleid,
        FieldId = f.id,
        FieldName = f.Name})
    .ToList();

ModuleField is navigation property in Field class. ModuleField是Field类中的导航属性。

EDIT: 编辑:
Models: 楷模:

public class Field
{
    [Key]
    public long Id { get; set; }

    public string FieldName { get; set; }

    [NotMapped]
    public bool IsModule 
    {
        get
        {
            return this.Module != null;
        }
    }

    public virtual Module Module {get; set;}
}

public class Module
{
    [Key]
    public long Id { get; set; }

    public string ModuleName { get; set; }

    public virtual IEnumerable<Field> Fields { get; set }
}

On model creating: 关于模型创建:

modelBuilder.Entity<Field>()
                .HasOptional(s => s.Module); // Marking property optional

Getting the Module.ModuleId if IsModule of Field is true: 如果字段的IsModule为true,则获取Module.ModuleId:

List<FieldDTO> result = _context.Fields
    .Include("Module")
    .Where(f => f.IsModule == true)
    .Select(f => new FieldDTO(){
        ModuleFieldModuleId = f.Module.Id,
        FieldId = f.Id,
        FieldName = f.Name})
    .ToList();

You could use linq join to return a List<FieldDTO> directly. 您可以使用linq join直接返回List<FieldDTO>

var result = (from e in _context.Fields
                     join mf in _context.ModuleFields on e.Id equals mf.FieldId
                     where e.IsModule == 1

                     select new FieldDTO
                     {
                         ModulefieldModuleid = mf.ModuleId,
                         FieldId = e.Id,
                         FieldName=e.Name


                     }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从不同的表中获取特定的列 ASP.NET 核心和实体框架核心 - How to get specific columns from different tables ASP.NET Core and Entity Framework Core 如何从 Entity Framework Core 获取实体保存顺序 - How to get entity save order from Entity Framework Core 如何使用 Entity Framework 在 .NET Core 中保存用户数据 - How to save user data in .NET Core with Entity Framework Entity Framework Core:使用包含从相关表中获取数据 - Entity Framework Core : get data from related tables with include .NET Core Entity Framework 如何从模型创建表 - How does .NET Core Entity Framework Create Tables From Model 实体框架相关表获取和保存数据 - Entity Framework Related Tables Get and Save Data 这是如何使用Entity Framework Core和ASP.NET Core MVC 2.2+和3.0创建数据传输对象(DTO) - Is This How to Create a Data Transfer Object (DTO) with Entity Framework Core & ASP.NET Core MVC 2.2+ and 3.0 如何对 dotnet core Entity Framework 中的许多表中的数据进行分组 - How to GroupBy data from many tables in dotnet core Entity Framework 从不同的表创建 ASP Net Core Entity Framework 实体 - ASP Net Core Entity Framework entity creation from different tables 如何在 ASP.NET Core web 应用程序中使用 Entity Framework Core 从数据库中保存和检索图像? - How to save and retrieve images from database using Entity Framework Core in ASP.NET Core web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM