Unable to convert System.Data.Entity.DbSet`1 to type ‘System.Data.Entity.DbSet`1.

huangapple go评论109阅读模式
英文:

Not able to convert System.Data.Entity.DbSet`1 to type 'System.Data.Entity.DbSet`1

问题

我已经创建了一个接口和一个实现了该接口的类,并且我正在尝试从数据库中查询后将接口引用分配给类对象。

  1. public interface IErrorCodeMapping : IEntity
  2. {
  3. int Id { get; set; }
  4. string Errcode { get; set; }
  5. bool FIBusiness { get; set; }
  6. bool equity { get; set; }
  7. }
  8. public class ErrCodeMapping : IErrorCodeMapping
  9. {
  10. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  11. public int Id { get; set; }
  12. [Key]
  13. public string Errcode { get; set; }
  14. public bool FIBusiness { get; set; }
  15. public bool Equity { get; set; }
  16. }
  17. IQueryable<IErrorCodeMapping> positions = (DbSet<IErrorCodeMapping>)domainRepository.Query<ErrCodeMapping>();

但我得到了以下错误:

  1. InvalidCastException: 无法将类型为 'System.Data.Entity.DbSet`1[Ex.Domain.Entities.ErrCodeMapping]' 的对象强制转换为类型 'System.Data.Entity.DbSet`1[Ex.Domain.Entities.IErrorCodeMapping]'
  2. 请问有人能告诉我如何执行此强制转换以及出了什么问题。
  3. **注意**: 我也尝试了其他转换方法,但似乎都不起作用。

请注意,这个错误是由于你尝试将一个实现类的对象转换为接口的对象,这是不允许的。要解决这个问题,你可以考虑使用LINQ进行投影,将结果映射到接口类型,而不是尝试进行强制转换。

英文:

I have created an interface and a class which implements that interface and I am trying to assign the interface reference the class object after querying it form the database

  1. public interface IErrorCodeMapping : IEntity
  2. {
  3. int Id { get; set; }
  4. string Errcode { get; set; }
  5. bool FIBusiness { get; set; }
  6. bool equity { get; set; }
  7. }
  8. public class ErrCodeMapping : IErrorCodeMapping
  9. {
  10. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  11. public int Id { get; set; }
  12. [Key]
  13. public string Errcode { get; set; }
  14. public bool FIBusiness { get; set; }
  15. public bool Equity { get; set; }
  16. }
  17. IQueryable&lt;IErrorCodeMapping&gt; positions = (DbSet&lt;IErrorCodeMapping&gt;)domainRepository.Query&lt;ErrCodeMapping&gt;();

But I am getting the following error

  1. InvalidCastException: Unable to cast object of type
  2. &#39;System.Data.Entity.DbSet`1[Ex.Domain.Entities.ErrCodeMapping]&#39; to type
  3. &#39;System.Data.Entity.DbSet`1[Ex.Domain.Entities.IErrorCodeMapping]&#39;.

Could someone please let me know how can I achieve this casting and what was going wrong.
Note: I have tried other casting approaches as well, but none of them seem to work.

答案1

得分: 1

需要将集合内部的元素进行类型转换,而不是集合本身。您可以使用Queryable.Cast&lt;TResult&gt;(IQueryable)来实现。在您的情况下,代码可能如下所示:

  1. IQueryable&lt;IErrorCodeMapping&gt; positions =
  2. domainRepository.Query&lt;ErrCodeMapping&gt;.Cast&lt;IErrorCodeMapping&gt;();
英文:

You need to cast the elements inside on the collection, not the collection itself. You can do that with Queryable.Cast&lt;TResult&gt;(IQueryable). In your case, that might look something like this:

  1. IQueryable&lt;IErrorCodeMapping&gt; positions =
  2. domainRepository.Query&lt;ErrCodeMapping&gt;.Cast&lt;IErrorCodeMapping&gt;();

huangapple
  • 本文由 发表于 2023年5月17日 20:51:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定