无法隐式转换 ICollection 类型

发布于 2025-01-10 01:18:24 字数 4798 浏览 0 评论 0原文

为了清楚地理解,这就是我想要实现的目标:我想要创建一个名为 TblMeter 的表,该表具有以下关系:

  • 与表 TblMeter 的一对一关系、
  • 与表 TblParameter 的一对多关系、
  • 一对多关系与表 TblRegister 的关系

这是我的数据模型:

public class TblMeterDetail
{
    [Key]
    public int ParamId { get; set; }
    public int ParamMeterId { get; set; }
    public TblMeter ParamMeter { get; set; }
    public virtual ICollection<TblParameter>  ParamParameter { get; set; }
    public virtual ICollection<TblRegister>  ParamRegister { get; set; }
    public bool IsArchive { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
}

我期望得到一个 MeterDetail,它有一个 Meter、一个 Parameter 列表,以及一个列表注册

运行 API 并尝试创建 MeterDetail 时,我在 Web api 中收到此错误:

无法隐式转换类型“System.Collections.Generic.ICollection”到“System.Collections.Generic.ICollection”。存在显式转换(您是否缺少强制转换?)

我应该如何正确地从 ICollection 转换为 ICollection

问题是由我的 EF 数据模型声明引起的吗?

DTO:

public class MeterDetailModel
{
    public int ParamId { get; set; }
    public int ParamMeterId { get; set; }
    public MeterModel ParamMeter { get; set; }
    public virtual ICollection<ParameterModel> ParamParameter { get; set; }
    public virtual ICollection<RegisterModel> ParamRegister { get; set; }
    public bool IsArchive { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
}

服务:

public TblMeterDetail GetMeterDetailById(int ParamId)
{
    return _db.MeterDetails.Find(ParamId);
}

这是用于通过 id 获取仪表详细信息的控制器/api:

        [HttpGet("api/meterdetail/{ParamId}")]
        public ActionResult GetTblMeterDetailById(int ParamId)
        {
            _logger.LogInformation("Getting Meter Detail based on Id");
            var meterDetail = _meterDetailService.GetMeterDetailById(ParamId);
            var meterDetailData = MeterDetailMapper.SerializeMeterDetailModel(meterDetail);
            return Ok(meterDetailData);
        }

错误位于映射器

映射器上:

public class MeterDetailMapper
    {
        public static MeterDetailModel SerializeMeterDetailModel(TblMeterDetail meterDetail)
        {
            return new MeterDetailModel
            {
                ParamId = meterDetail.ParamId,
                ParamMeter = MapMeter(meterDetail.ParamMeter),
                ParamParameter = (ICollection<ParameterModel>)meterDetail.ParamParameter, <- error here
                ParamRegister = (ICollection<RegisterModel>)meterDetail.ParamRegister, <- error here
                IsArchive = meterDetail.IsArchive,
                CreatedOn = meterDetail.CreatedOn,
                UpdatedOn = meterDetail.UpdatedOn,
            };

        }

        public static TblMeterDetail SerializeMeterDetailModel(MeterDetailModel meterDetail)
        {
            return new TblMeterDetail
            {
                ParamId = meterDetail.ParamId,
                ParamMeter = MapMeter(meterDetail.ParamMeter),
                ParamParameter = (ICollection<TblParameter>)meterDetail.ParamParameter, <- error here
                ParamRegister = (ICollection<TblRegister>)meterDetail.ParamRegister, <- error here
                IsArchive = meterDetail.IsArchive,
                CreatedOn = meterDetail.CreatedOn,
                UpdatedOn = meterDetail.UpdatedOn,
            };

        }

        public static MeterModel MapMeter(TblMeter meter)
        {
            return new MeterModel
            {
                ParamId = meter.ParamId,
                ParamMeterId = meter.ParamMeterId,
                ParamMeterName = meter.ParamMeterName,
                ParamDeviceType = meter.ParamDeviceType,
                IsArchive = meter.IsArchive,
                CreatedOn = meter.CreatedOn,
                UpdatedOn = meter.UpdatedOn,
            };

        }
        public static TblMeter MapMeter(MeterModel meter)
        {
            return new TblMeter
            {
                ParamId = meter.ParamId,
                ParamMeterId = meter.ParamMeterId,
                ParamMeterName = meter.ParamMeterName,
                ParamDeviceType = meter.ParamDeviceType,
                IsArchive = meter.IsArchive,
                CreatedOn = meter.CreatedOn,
                UpdatedOn = meter.UpdatedOn,
            };

        }
    }

For a clear understanding, this is what I'm trying to achieve: I want to make a have a table called TblMeter that has a relationship of:

  • one to one relationship to table TblMeter,
  • one to many relationships to table TblParameter,
  • one to many relationships to table TblRegister

This is my data model:

public class TblMeterDetail
{
    [Key]
    public int ParamId { get; set; }
    public int ParamMeterId { get; set; }
    public TblMeter ParamMeter { get; set; }
    public virtual ICollection<TblParameter>  ParamParameter { get; set; }
    public virtual ICollection<TblRegister>  ParamRegister { get; set; }
    public bool IsArchive { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
}

What I'm expecting is to get a MeterDetail that has one Meter, a list of Parameter, and a list of Register.

When running the API and trying to create a MeterDetail, I get this error in my web api:

Cannot implicitly convert type 'System.Collections.Generic.ICollection<OamrBackend.Web.ViewModels.RegisterModel>' to 'System.Collections.Generic.ICollection<OamrBackend.Data.Models.TblRegister>'. An explicit conversion exists (are you missing a cast?)

How should I convert from ICollection<TblParameter> to ICollection<ParameterModel> correctly?

Is the problem caused by my EF Data Model declaration?

DTO:

public class MeterDetailModel
{
    public int ParamId { get; set; }
    public int ParamMeterId { get; set; }
    public MeterModel ParamMeter { get; set; }
    public virtual ICollection<ParameterModel> ParamParameter { get; set; }
    public virtual ICollection<RegisterModel> ParamRegister { get; set; }
    public bool IsArchive { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
}

Service:

public TblMeterDetail GetMeterDetailById(int ParamId)
{
    return _db.MeterDetails.Find(ParamId);
}

This is the controller/api for getting meter detail by id:

        [HttpGet("api/meterdetail/{ParamId}")]
        public ActionResult GetTblMeterDetailById(int ParamId)
        {
            _logger.LogInformation("Getting Meter Detail based on Id");
            var meterDetail = _meterDetailService.GetMeterDetailById(ParamId);
            var meterDetailData = MeterDetailMapper.SerializeMeterDetailModel(meterDetail);
            return Ok(meterDetailData);
        }

The error is here on the mapper

Mapper:

public class MeterDetailMapper
    {
        public static MeterDetailModel SerializeMeterDetailModel(TblMeterDetail meterDetail)
        {
            return new MeterDetailModel
            {
                ParamId = meterDetail.ParamId,
                ParamMeter = MapMeter(meterDetail.ParamMeter),
                ParamParameter = (ICollection<ParameterModel>)meterDetail.ParamParameter, <- error here
                ParamRegister = (ICollection<RegisterModel>)meterDetail.ParamRegister, <- error here
                IsArchive = meterDetail.IsArchive,
                CreatedOn = meterDetail.CreatedOn,
                UpdatedOn = meterDetail.UpdatedOn,
            };

        }

        public static TblMeterDetail SerializeMeterDetailModel(MeterDetailModel meterDetail)
        {
            return new TblMeterDetail
            {
                ParamId = meterDetail.ParamId,
                ParamMeter = MapMeter(meterDetail.ParamMeter),
                ParamParameter = (ICollection<TblParameter>)meterDetail.ParamParameter, <- error here
                ParamRegister = (ICollection<TblRegister>)meterDetail.ParamRegister, <- error here
                IsArchive = meterDetail.IsArchive,
                CreatedOn = meterDetail.CreatedOn,
                UpdatedOn = meterDetail.UpdatedOn,
            };

        }

        public static MeterModel MapMeter(TblMeter meter)
        {
            return new MeterModel
            {
                ParamId = meter.ParamId,
                ParamMeterId = meter.ParamMeterId,
                ParamMeterName = meter.ParamMeterName,
                ParamDeviceType = meter.ParamDeviceType,
                IsArchive = meter.IsArchive,
                CreatedOn = meter.CreatedOn,
                UpdatedOn = meter.UpdatedOn,
            };

        }
        public static TblMeter MapMeter(MeterModel meter)
        {
            return new TblMeter
            {
                ParamId = meter.ParamId,
                ParamMeterId = meter.ParamMeterId,
                ParamMeterName = meter.ParamMeterName,
                ParamDeviceType = meter.ParamDeviceType,
                IsArchive = meter.IsArchive,
                CreatedOn = meter.CreatedOn,
                UpdatedOn = meter.UpdatedOn,
            };

        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独留℉清风醉 2025-01-17 01:18:24

SerializeMeterDetailModel 中,您传入一个 TblMeterDetail 对象。 TblMeterDetail 具有 ParamRegister 的属性,其类型为 ICollection。但随后在 SerializeMeterDetailModel 中,您将 ParamRegister 作为类型 ICollection 读取。这就是为什么您会遇到类型不匹配/无法转换的原因。换句话说,该属性作为一种类型传入,但您试图将其作为不同的类型进行处理。

老实说,我不太清楚你在做什么,也不知道你所谓的“数据模型”和“DTO”之间有什么区别。也许“DTO”是视图模型?无论如何,您需要将传入类型转换为所需类型。

您可以在 DTO 类型的构造函数中执行此操作(即,如果您要在多个中执行此操作,则将 TblMeterDetail 对象传递给 MeterDetailModel 的构造函数地方,或者在您的 SerializeMeterDetailModel 方法中(如果它刚刚发生在那里),即:

 ParamRegister = meterDetail.ParamRegister
     .Select(i=>new RegisterModel{
        propertyA = i.propertyA,
        propertyB = i.propertyB,
        etc}
     ).ToList(); <- error here

In SerializeMeterDetailModel, you are passing in a TblMeterDetail object. TblMeterDetail has a property of ParamRegister that is of type ICollection<TblRegister>. But then in the SerializeMeterDetailModel you are reading ParamRegister as type ICollection<RegisterModel>. That's why you're getting the type mismatch / unable to convert. In other words, that property is being passed in as one type, but you're trying to handle it as a different type.

I honestly don't know quite what you're doing, and what the difference between what you call your 'data model' and your 'DTO' are. Maybe the 'DTO's are view models? Regardless, you will need to translate the incoming types to the desired types.

You can do this in the constructor of the DTO type (ie, pass in a TblMeterDetail object to the constructor of MeterDetailModel if you're going to do it in more than one place, or in your SerializeMeterDetailModel method if it's just happening there. ie something like:

 ParamRegister = meterDetail.ParamRegister
     .Select(i=>new RegisterModel{
        propertyA = i.propertyA,
        propertyB = i.propertyB,
        etc}
     ).ToList(); <- error here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文