简体   繁体   English

如何在 ASP.NET CORE 3.1.1 中具有 ForeignKey 的 Map(使用 AutoMapper)实体(C#,EntityFrameworkCore)

[英]How to Map (using AutoMapper) entities that have a ForeignKey in ASP.NET CORE 3.1.1 (C#,EntityFrameworkCore)

I have this function in my controller that creates a an entity:我的 controller 中有这个 function ,它创建了一个实体:

    [HttpPost]
    [ProducesResponseType(typeof(ConnectionDBResponse), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<ActionResult<ConnectionDBResponse>> PostConnectionDB([FromBody] CreateConnectionDBQuery query)
    {
        var connectionDBs = _mapper.Map<ConnectionDBDataModel>(query);
        _context.ConnectionDB.Add(connectionDBs);
        await _context.SaveChangesAsync();

        var connectionDBResponse = _mapper.Map<ConnectionDBResponse>(connectionDBs);
        return CreatedAtAction(nameof(GetAllConnectionDB), new { id = connectionDBs.Id }, connectionDBResponse);
    }

For that I'm mapping between these two classes: The response Class:为此,我在这两个类之间进行映射:响应 Class:

public class CreateConnectionDBQuery
{
    public string ServerType { get; set; }
    public string ServerName { get; set; }
    public string port { get; set; }
    public string AuthType { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string DBName { get; set; }
    public string FolderName { get; set; }
    public ScheduleConfigResponse ScheduleConfig { get; set; }
    public Boolean hasEmails { get; set; }
    public EmailConfigResponse EmailConfig { get; set; }
}

public class CreateScheduleConfigQuery
{
    public string HourOfSave { get; set; }
    public int NumDaysInDB { get; set; }
    public CreateConnectionDBQuery ConnDB { get; set; }
    public int ConnDBForeignKey { get; set; }
}

public class CreateEmailConfigQuery
{
    public string SuccesEmail { get; set; }
    public string FailureEmail { get; set; }
    public CreateConnectionDBQuery ConnDB { get; set; }
    public int ConnDBForeignKey { get; set; }
}

And the dataModel Class:数据型号 Class:

[Table("ConnectionDB")]
public class ConnectionDBDataModel
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string ServerType { get; set; }
    [Required]
    public string ServerName { get; set; }
    public string port { get; set; }
    public string AuthType { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    [Required]
    public string DBName { get; set; }
    [Required]
    public string FolderName { get; set; }
    public ScheduleConfigDataModel ScheduleConfig { get; set; }
    public Boolean hasEmails { get; set; }
    public EmailConfigDataModel EmailConfig { get; set; }
}

[Table("ScheduleConfig")]
public class ScheduleConfigDataModel
{
    [Key]
    public int Id { get; set; }
    public string HourOfSave { get; set; }
    public int NumDaysInDB { get; set; }

    public int ConnDBForeignKey { get; set; }
    public ConnectionDBDataModel ConnDB { get; set; }
}

[Table("EmailConfig")]
public class EmailConfigDataModel
{
    [Key]
    public int Id { get; set; }
    public string SuccesEmail { get; set; }
    public string FailureEmail { get; set; }

    public int ConnDBForeignKey { get; set; }
    public ConnectionDBDataModel ConnDB { get; set; }
}

For that I'm using the AutoMapper as following:为此,我使用 AutoMapper 如下:

        #region ConnectionDB

        CreateMap<ConnectionDBDataModel, ConnectionDBResponse>();

        CreateMap<CreateConnectionDBQuery, ConnectionDBDataModel>()
            .ForMember(dest => dest.Id, opt => opt.Ignore());

        CreateMap<UpdateConnectionDBQuery, ConnectionDBDataModel>()
            .ForMember(dest => dest.Id, opt => opt.Ignore());

        #endregion

        #region ScheduleConfig

        CreateMap<ScheduleConfigDataModel, ScheduleConfigResponse>()
            .ForMember(dest => dest.ConnDBForeignKey, opt => opt.Ignore());

        CreateMap<CreateScheduleConfigQuery, ScheduleConfigDataModel>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForMember(dest => dest.ConnDBForeignKey, opt => opt.Ignore());

        CreateMap<UpdateScheduleConfigQuery, ScheduleConfigDataModel>()
            .ForMember(dest => dest.Id, opt => opt.Ignore());

        #endregion ScheduleConfig

        #region EmailConfig

        CreateMap<EmailConfigDataModel, EmailConfigResponse>()
            .ForMember(dest => dest.ConnDBForeignKey, opt => opt.Ignore());

        CreateMap<CreateEmailConfigQuery, EmailConfigDataModel>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForMember(dest => dest.ConnDBForeignKey, opt => opt.Ignore());

        CreateMap<UpdateEmailConfigQuery, EmailConfigDataModel>()
            .ForMember(dest => dest.Id, opt => opt.Ignore());

        #endregion

But when I try to create this element it gives me an error saying that it's coming from an invalid Mapping as shown in the screen bellow:但是当我尝试创建这个元素时,它给了我一个错误,说它来自一个无效的映射,如下面的屏幕所示:

在此处输入图像描述

I have tried to Ignore the Foreign Key (because my guess that this problem is coming from the foreignKey) using this line of code: .ForMember(dest => dest.ConnDBForeignKey, opt => opt.Ignore());我尝试使用以下代码行忽略外键(因为我猜这个问题来自外键): .ForMember(dest => dest.ConnDBForeignKey, opt => opt.Ignore()); , but I guess it's not the way to solve that problem. ,但我想这不是解决这个问题的方法。 Any help would be appreciated thank you!任何帮助将不胜感激,谢谢!

The error is happening because when mapping CreateConnectionDBQuery to ConnectionDBDataModel , there is no mapping defined for the types of the ScheduleConfig properties.发生错误是因为在将CreateConnectionDBQuery映射到ConnectionDBDataModel时,没有为ScheduleConfig属性的类型定义映射。

I'm guessing that in your CreateConnectionDBQuery , your ScheduleConfig property should be of type CreateScheduleConfigQuery instead of ScheduleConfigResponse .我猜在您的CreateConnectionDBQuery中,您的ScheduleConfig属性应该是CreateScheduleConfigQuery而不是ScheduleConfigResponse类型。

Alternatively, if you don't want to change the models, you could add a mapping configuration from ScheduleConfigResponse to ScheduleConfigDataModel .或者,如果您不想更改模型,您可以添加从ScheduleConfigResponseScheduleConfigDataModel的映射配置。 But that doesn't seem very intuitive.但这似乎不是很直观。

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

相关问题 如何创建在每个给定时间段运行一个函数的 BackGround 服务? 使用 C#(asp.net 核心 3.1.1) - How can I create a BackGround service that runs a function every given period of time ? Using C# (asp.net core 3.1.1) 如何在ASP.Net Core 2中使用AutoMapper将Model对象实际映射到ViewModel对象? - How to actually map Model object into a ViewModel object using AutoMapper in ASP.Net core 2? 在C#asp.net性能实践中使用静态自动映射器 - Using static automapper in C# asp.net performance practices 将 Automapper 与 ASP.NET Core 结合使用 - Using Automapper with ASP.NET Core ASP.Net MVC C#AutoMapper使用Where语句 - ASP.Net MVC C# AutoMapper using Where statement 如何从 ASP.NET Core 3.1 中的 EntityFrameworkCore 3 中删除依赖项 - How to remove dependency from EntityFrameworkCore 3 in ASP.NET core 3.1 带有 EntityFrameworkCore 的 ASP.NET Core 中的 SQLite - SQLite in ASP.NET Core with EntityFrameworkCore C# Asp.Net EntityFrameworkCore 5.0 _ManageNav.cshtml.g.cs 删除 using 重建语句 - C# Asp.Net EntityFrameworkCore 5.0 _ManageNav.cshtml.g.cs deletes using statement on rebuild 如何根据 asp.net 核心中 AutoMapper 中的用户角色有条件地映射/忽略属性 - How to conditionally map/ignore properties based on user role in AutoMapper in asp.net core C# .NET 核心 3 MVC 脚手架与 EntityFrameworkCore - C# .NET core 3 MVC Scaffolding with EntityFrameworkCore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM